I am try to get my path from SaveFileDialog
without my file name in order to create a DirectoryInfo
object
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;
}
}
您也可以使用System.IO.Path.GetDirectoryName
来实现此目的
System.IO.Path.GetDirectoryName(filePath)
Use System.IO.FileInfo.DirectoryName property to get the full path of the directory of a file.
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);
string fileName = @"C:\TMP\log.txt";
FileInfo fileInfo = new FileInfo(fileName);
Console.WriteLine(fileInfo.DirectoryName);
您可以使用System.IO.Path.GetDirectoryName方法:
Console.WriteLine(System.IO.Path.GetDirectoryName(Filename));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.