C#文件路径帮助

C#文件路径帮助,c#,winforms,openfiledialog,C#,Winforms,Openfiledialog,我使用OpenFileDialog搜索特定文件。当用户选择文件时,我希望将该路径存储在变量中。然而,这些似乎不是OpenFileDialog中的一个选项 有人知道怎么做吗 谢谢 编辑:这是Winforms,我不想保存包含文件名的路径,只保存文件所在的位置。对话框关闭后,OpenFileDialog对象上应该有一个文件路径(或类似的内容)属性,它将存储用户输入的任何文件路径。对话框关闭后,OpenFileDialog对象上应该有一个文件路径(或类似的)属性,它将存储用户输入的任何文件路径。如果您使

我使用OpenFileDialog搜索特定文件。当用户选择文件时,我希望将该路径存储在变量中。然而,这些似乎不是OpenFileDialog中的一个选项

有人知道怎么做吗

谢谢


编辑:这是Winforms,我不想保存包含文件名的路径,只保存文件所在的位置。

对话框关闭后,OpenFileDialog对象上应该有一个文件路径(或类似的内容)属性,它将存储用户输入的任何文件路径。

对话框关闭后,OpenFileDialog对象上应该有一个文件路径(或类似的)属性,它将存储用户输入的任何文件路径。

如果您使用的是WinForms,使用
OpenFileDialog
实例的
FileName
属性。

如果您使用的是WinForms,请使用
OpenFileDialog
实例的
FileName
属性。

在WinForms上:

String fileName;
OpenFileDialog ofd = new OpenFileDialog();
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.Ok) {
  fileName = ofd.FileName;
}

//getting only the path:
String path = fileName.Substring(0, fileName.LastIndexOf('\\'));

//or easier (thanks to Aaron)
String path = System.IO.Path.GetDirectoryName(fileName);
在WinForms上:

String fileName;
OpenFileDialog ofd = new OpenFileDialog();
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.Ok) {
  fileName = ofd.FileName;
}

//getting only the path:
String path = fileName.Substring(0, fileName.LastIndexOf('\\'));

//or easier (thanks to Aaron)
String path = System.IO.Path.GetDirectoryName(fileName);

试试文件名。或文件名,如果允许选择多个文件(Multiselect=true)

请尝试文件名。或者文件名如果您允许选择多个文件(Multiselect=true)

而不是从MSDN复制粘贴答案,我将只链接到它们

MSDN的文档

MSDN文档打开


在发布问题之前,请尝试寻找答案。

我不会从MSDN复制粘贴答案,而是链接到答案

MSDN的文档

MSDN文档打开


在发布问题之前,请尝试寻找答案。

您存储了路径。。。去别的地方

我通常做的是创建一个用户范围的配置变量

以下是其使用示例:

var filename = Properties.Settings.Default.LastDocument;
var sfd = new Microsoft.Win32.SaveFileDialog();
sfd.FileName = filename;
/* configure SFD */
var result = sfd.ShowDialog() ?? false;
if (!result)
    return;
/* save stuff here */
Properties.Settings.Default.LastDocument = filename;
Properties.Settings.Default.Save();


要仅保存目录,请使用存储路径。。。去别的地方

我通常做的是创建一个用户范围的配置变量

以下是其使用示例:

var filename = Properties.Settings.Default.LastDocument;
var sfd = new Microsoft.Win32.SaveFileDialog();
sfd.FileName = filename;
/* configure SFD */
var result = sfd.ShowDialog() ?? false;
if (!result)
    return;
/* save stuff here */
Properties.Settings.Default.LastDocument = filename;
Properties.Settings.Default.Save();


要仅保存目录,请使用

这将根据
OpenFileDialog
FileName
属性检索路径

String path = System.IO.Path.GetDirectoryName(OpenFileDialog.FileName);

这将根据
OpenFileDialog
FileName
属性检索您的路径

String path = System.IO.Path.GetDirectoryName(OpenFileDialog.FileName);

该财产不存在于SL中,仅供参考;不确定OP正在使用什么…最后字符串path=System.IO.path.GetDirectoryName(文件名);该财产不存在于SL中,仅供参考;不确定OP正在使用什么…最后字符串path=System.IO.path.GetDirectoryName(文件名);这包括我不想要的文件名。只是文件所在的路径。我应该说得更清楚一点,不需要再串了;字符串路径=System.IO.path.GetDirectoryName(文件名);这包括我不想要的文件名。只是文件所在的路径。我应该说得更清楚一点,不需要再串了;字符串路径=System.IO.path.GetDirectoryName(文件名);谢谢,你最后的建议为我指明了正确的方向。我使用了System.IO.Directory.GetParent(openFileDialog1.FileName).ToString();这是完美的。谢谢,谢谢,你最后的建议给我指明了正确的方向。我使用了System.IO.Directory.GetParent(openFileDialog1.FileName).ToString();这是完美的。谢谢