.net 如何从 FileDialog.FileName 返回的路径中删除文件名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4895726/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:12:48  来源:igfitidea点击:

How can I remove the file name from the path returned by FileDialog.FileName?

vb.netfile-iofile.net

提问by SpongeBob SquarePants

How can I remove the actual file name from the path returned by the FileNameproperty of an Open or Save File Dialog?

如何从FileName打开或保存文件对话框的属性返回的路径中删除实际文件名?

All I want is the path to the file withoutthe file name.

我想要的只是没有文件名的文件路径。

回答by Cody Gray

Pass the full path (the one including the file name) to the System.IO.Path.GetDirectoryNamemethod. This will strip out the file name and return the full path to the directory containing that file.

将完整路径(包括文件名的路径)传递给System.IO.Path.GetDirectoryName方法。这将去掉文件名并返回包含该文件的目录的完整路径。

For example:

例如:

Dim filePath As String = "C:\MyDir\MySubDir\myfile.ext"
Dim directoryPath As String = Path.GetDirectoryName(filePath)

Places the following string in the directoryPathvariable:

将以下字符串放入directoryPath变量中:

C:\MyDir\MySubDir

C:\MyDir\MySubDir

回答by zara

Dim dir As String = System.Environment.GetFolderPath(Environment.SpecialFolderOption.None)

The value of dirwill give you the path only.

的值dir只会给你路径。

回答by Mohd Siddique Merchant

My.Computer.FileSystem.GetFileInfo(sFullFileName).DirectoryName

My.Computer.FileSystem.GetFileInfo(sFullFileName).DirectoryName