从OpenFileDialog路径/文件名提取路径

时间:2022-10-07 21:27:50

I'm writing a little utility that starts with selecting a file, and then I need to select a folder. I'd like to default the folder to where the selected file was.

我正在编写一个小实用程序,从选择一个文件开始,然后我需要选择一个文件夹。我想将文件夹默认为所选文件所在的位置。

OpenFileDialog.FileName returns the full path & filename - what I want is to obtain just the path portion (sans filename), so I can use that as the initial selected folder.

OpenFileDialog。FileName返回完整的路径和文件名——我想要的是获取路径部分(sans FileName),所以我可以将它作为初始选择的文件夹。

    private System.Windows.Forms.OpenFileDialog ofd;
    private System.Windows.Forms.FolderBrowserDialog fbd;
    ...
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        string sourceFile = ofd.FileName;
        string sourceFolder = ???;
    }
    ...
    fbd.SelectedPath = sourceFolder; // set initial fbd.ShowDialog() folder
    if (fbd.ShowDialog() == DialogResult.OK)
    {
       ...
    }

Are there any .NET methods to do this, or do I need to use regex, split, trim, etc??

有什么。net方法可以做到这一点吗?或者我需要使用regex、split、trim等等?

5 个解决方案

#1


91  

Use the Path class from System.IO. It contains useful calls for manipulating file paths, including GetDirectoryName which does what you want, returning the directory portion of the file path.

使用System.IO中的Path类。它包含操作文件路径的有用调用,包括GetDirectoryName,它执行您想要的操作,返回文件路径的目录部分。

Usage is simple.

使用很简单。

string directoryPath = Path.GetDirectoryName(filePath);

#2


21  

how about this:

这个怎么样:

string fullPath = ofd.FileName;
string fileName = ofd.SafeFileName;
string path = fullPath.Replace(fileName, "");

#3


12  

if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
    strfilename = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
}

#4


4  

You can use FolderBrowserDialog instead of FileDialog and get the path from the OK result.

您可以使用FolderBrowserDialog而不是FileDialog从OK结果中获取路径。

FolderBrowserDialog browser = new FolderBrowserDialog();
string tempPath ="";

if (browser.ShowDialog() == DialogResult.OK)
{
  tempPath  = browser.SelectedPath; // prints path
}

#5


0  

Here's the simple way to do It !

这里有一个简单的方法!

string fullPath =openFileDialog1.FileName;
string directory;
directory = fullPath.Substring(0, fullPath.LastIndexOf('\\'));

#1


91  

Use the Path class from System.IO. It contains useful calls for manipulating file paths, including GetDirectoryName which does what you want, returning the directory portion of the file path.

使用System.IO中的Path类。它包含操作文件路径的有用调用,包括GetDirectoryName,它执行您想要的操作,返回文件路径的目录部分。

Usage is simple.

使用很简单。

string directoryPath = Path.GetDirectoryName(filePath);

#2


21  

how about this:

这个怎么样:

string fullPath = ofd.FileName;
string fileName = ofd.SafeFileName;
string path = fullPath.Replace(fileName, "");

#3


12  

if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
    strfilename = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
}

#4


4  

You can use FolderBrowserDialog instead of FileDialog and get the path from the OK result.

您可以使用FolderBrowserDialog而不是FileDialog从OK结果中获取路径。

FolderBrowserDialog browser = new FolderBrowserDialog();
string tempPath ="";

if (browser.ShowDialog() == DialogResult.OK)
{
  tempPath  = browser.SelectedPath; // prints path
}

#5


0  

Here's the simple way to do It !

这里有一个简单的方法!

string fullPath =openFileDialog1.FileName;
string directory;
directory = fullPath.Substring(0, fullPath.LastIndexOf('\\'));