vb.net 使用 openfiledialog 获取完整路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30308485/
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
get full path using openfiledialog
提问by jsteffler
Simple utility to get a file for use.
获取文件以供使用的简单实用程序。
Using openfiledialog I am trying to get the full path EG: File = text1.txt and it is located in c:\temp. So the full path is C:\temp\text1.txt.
使用 openfiledialog 我试图获取完整路径 EG: File = text1.txt 并且它位于 c:\temp。所以完整路径是C:\temp\text1.txt。
But all I can get is the file name. I've searched I've hunted, I'e tried for a couple of hours and nothing works.
但我能得到的只是文件名。我搜索过我打猎过,我尝试了几个小时,但没有任何效果。
Here is code with comments...
这是带注释的代码...
'open the openfile dialog so the user can search for a file
Dim openFileDialog1 As New OpenFileDialog()
'set the root to the z drive
openFileDialog1.InitialDirectory = "Z:\"
'make sure the root goes back to where the user started
openFileDialog1.RestoreDirectory = True
'show the dialog
openFileDialog1.ShowDialog()
'check there is something to work with... the user did not exit before selecting a file etc.
If openFileDialog1.FileName.Length = 0 Then
'if the user selected a file set the value of the replacefile text box
Else
TB_ReplacementFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
End If
All I get is the file name...
我得到的只是文件名...
回答by jsteffler
The MSDN documentation and numerous posts all over the place say all you need is openfiledialog.FileName. However this did not work for me, can't tell you why. What DID work is to use this:
MSDN 文档和随处可见的大量帖子说您只需要 openfiledialog.FileName。但是,这对我不起作用,无法告诉您原因。DID 的工作是使用这个:
TB_ReplacementFile.Text = System.IO.Path.GetFullPath(openFileDialog1.FileName)
This works great, I get what I need. I cannot explain why I have to do this. Not sure how I can be the problem, but that must be the problem right?!
这很好用,我得到了我需要的东西。我无法解释为什么我必须这样做。不知道我怎么会是问题,但这一定是问题吧?!
Hopefully this helps someone.
希望这对某人有所帮助。
回答by Justin Ryan
The FileName Propertyreturns the full path.
该FileName属性返回的完整路径。
The file name includes both the file path and the extension. If no files are selected, this method returns an empty string ("").
文件名包括文件路径和扩展名。如果未选择任何文件,此方法将返回一个空字符串 ("")。
If (openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then
TB_ReplacementFile.Text = openFileDialog1.FileName
End If
回答by AntiTcb
Not 100% sure if this would resolve the issue you're having, or if it's simply just another way to handle it, but I prefer to check the DialogResult. I.E:
不能 100% 确定这是否会解决您遇到的问题,或者这只是另一种处理方式,但我更喜欢检查 DialogResult。IE:
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "Z:\"
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.Ok Then
Console.WriteLine(openFileDialog1.fileName)
End If
回答by Raga
this worked to get the directory or path dim sDir as String = System.IO.Path.GetDirectoryName(openfiledialog1.FileName.ToString)
这有助于将目录或路径 dim sDir 设为 String = System.IO.Path。GetDirectoryName(openfiledialog1.FileName.ToString)

