4

我试图从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;

    }
}
4

4 回答 4

9

您也可以System.IO.Path.GetDirectoryName用于此目的

System.IO.Path.GetDirectoryName(filePath)
于 2012-10-06T14:45:35.023 回答
2

使用System.IO.FileInfo.DirectoryName属性获取文件目录的完整路径。


string fileName = @"C:\TMP\log.txt";
FileInfo fileInfo = new FileInfo(fileName);

Console.WriteLine(fileInfo.DirectoryName); // Output: "C:\TMP"

使用您的示例:

string str = saveFileDialogBrowse2.FileName;

FileInfo fileInfo = new FileInfo(str);
Console.WriteLine(fileInfo.DirectoryName);
于 2012-10-06T14:33:18.820 回答
2
string fileName = @"C:\TMP\log.txt";
FileInfo fileInfo = new FileInfo(fileName);

Console.WriteLine(fileInfo.DirectoryName);
于 2012-10-06T14:36:55.387 回答
1

您可以使用System.IO.Path.GetDirectoryName方法:

Console.WriteLine(System.IO.Path.GetDirectoryName(Filename));
于 2012-10-06T14:43:47.067 回答