VB.NET 在 OpenFileDialog 中获取所选文件的文件名 - 无路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16653897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET get file name of selected file in OpenFileDialog - no path
提问by TypeM1smatch
I'm using the OpenFileDialog to open an Excel workbook, pass data from that workbook to another workbook, and then close the workbook I opened via OpenFileDialog. My problem is passing the data...how do I obtain just the name of the file I've opened via OpenFileDialog? I can't have the path, I just need the name of the file with it's extension. Here's part of my code
我正在使用 OpenFileDialog 打开 Excel 工作簿,将数据从该工作簿传递到另一个工作簿,然后关闭我通过 OpenFileDialog 打开的工作簿。我的问题是传递数据...如何仅获取我通过 OpenFileDialog 打开的文件的名称?我没有路径,我只需要带有扩展名的文件名。这是我的代码的一部分
Dim filedialog As OpenFileDialog = New OpenFileDialog()
Dim strFileName As String
filedialog.Title = "Open File Dialog"
filedialog.InitialDirectory = "W:\TOM\ERIC\NET Dev"
filedialog.RestoreDirectory = True
If filedialog.ShowDialog() = DialogResult.OK Then
strFileName = filedialog.FileName
System.Diagnostics.Process.Start(strFileName)
StatVar.xlApp.Workbooks(StatVar.workbookName).Sheets("Input Form Info").Range("B4:B204").Value = StatVar.xlApp.Workbooks(strFileName).Sheets("DJA Corp Use").Range("C2:C202").Value
StatVar.xlApp.Workbooks(strFileName).Close(False)
End If
Else
Exit Sub
End If
I can't pass my data from workbook to workbook because the strFileName variable contains the file path. I've been trying to work with this function to return the file name but I'm not experienced enough apparently. Any help is appreciated.
我无法将我的数据从工作簿传递到工作簿,因为 strFileName 变量包含文件路径。我一直在尝试使用此函数返回文件名,但显然我经验不足。任何帮助表示赞赏。
Public Function FileNameWithoutPath(ByVal FullPath As String) As String
Return System.IO.Path.GetFileName(FullPath).ToString
End Function
采纳答案by maxedev
回答by Idle_Mind
You might need to leave it as the full path for Process.Start(), then afterwards remove the path so it's just the filename:
您可能需要将其保留为 Process.Start() 的完整路径,然后删除该路径,使其只是文件名:
If filedialog.ShowDialog() = DialogResult.OK Then
strFileName = filedialog.FileName
System.Diagnostics.Process.Start(strFileName)
strFileName = System.IO.Path.GetFileName(strFileName)
StatVar.xlApp.Workbooks(StatVar.workbookName).Sheets("Input Form Info").Range("B4:B204").Value = StatVar.xlApp.Workbooks(strFileName).Sheets("DJA Corp Use").Range("C2:C202").Value
StatVar.xlApp.Workbooks(strFileName).Close(False)
End If
回答by Bh Rh
Get the File Name of Open file Dialog then Use System.IO.Path.GetFileName(OpenFileDialog1.FileName)
获取打开文件对话框的文件名,然后使用 System.IO.Path.GetFileName(OpenFileDialog1.FileName)