我使用OpenFileDialog来搜索特定的文件。当用户选择文件时,我希望将该路径存储在一个变量中。然而,在OpenFileDialog中,这些似乎不是一个选项?
有人知道怎么做吗?
谢谢。
编辑:这是Winforms,我不想保存包含文件名的路径,只保存文件所在的位置。
发布于 2011-02-09 23:21:02
这将根据OpenFileDialog的FileName属性检索您的路径。
String path = System.IO.Path.GetDirectoryName(OpenFileDialog.FileName);发布于 2011-02-09 23:08:45
如果使用的是WinForms,请使用OpenFileDialog实例的FileName属性。
发布于 2011-02-09 23:10:17
在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);https://stackoverflow.com/questions/4946655
复制相似问题