C# 从saveFileDialog获取没有文件名的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12760689/
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 path without file name from saveFileDialog?
提问by user1710944
I am try to get my path from SaveFileDialogwithout my file name in order to create a DirectoryInfoobject
我试图从SaveFileDialog没有我的文件名的情况下获取我的路径以创建一个DirectoryInfo对象
private void btnBrowseCapture_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialogBrowse2 = new SaveFileDialog();
saveFileDialogBrowse2.Filter = "Pcap file|*.pcap";
saveFileDialogBrowse2.Title = "Save an pcap File";
saveFileDialogBrowse2.ShowDialog();
if (saveFileDialogBrowse2.FileName != "")
{
string str = saveFileDialogBrowse2.FileName;
}
}
回答by sergserg
Use System.IO.FileInfo.DirectoryNameproperty to get the full path of the directory of a file.
使用System.IO.FileInfo.DirectoryName属性获取文件目录的完整路径。
string fileName = @"C:\TMP\log.txt";
FileInfo fileInfo = new FileInfo(fileName);
Console.WriteLine(fileInfo.DirectoryName); // Output: "C:\TMP"
Using your example:
使用您的示例:
string str = saveFileDialogBrowse2.FileName;
FileInfo fileInfo = new FileInfo(str);
Console.WriteLine(fileInfo.DirectoryName);
回答by Sumudu Kurukulasuriya
string fileName = @"C:\TMP\log.txt";
FileInfo fileInfo = new FileInfo(fileName);
Console.WriteLine(fileInfo.DirectoryName);
回答by U1199880
You can use System.IO.Path.GetDirectoryNamemethod:
您可以使用System.IO.Path.GetDirectoryName方法:
Console.WriteLine(System.IO.Path.GetDirectoryName(Filename));
回答by dotINSolution
You can also use System.IO.Path.GetDirectoryNamefor this purpose
您也可以System.IO.Path.GetDirectoryName为此目的使用
System.IO.Path.GetDirectoryName(filePath)

