简体   繁体   English

文件对话框获取完整路径

[英]File Dialog Get Full Path

I have a OpenFileDialog to which I just need a Full Path such eg "C:\\Users\\Tshililo\\example.csv".我有一个 OpenFileDialog,我只需要一个完整路径,例如“C:\\Users\\Tshililo\\example.csv”。 How do I get the path after selecting a file?选择文件后如何获取路径?

I've tried using System.IO.Path.GetFileName(ofd.File.Name);我试过使用System.IO.Path.GetFileName(ofd.File.Name); but the output is "example.csv".但输出是“example.csv”。

    OpenFileDialog ofd = new OpenFileDialog();
    using (var stream = ofd.File.OpenRead())
            {
      var thiname = System.IO.Path.GetFileName(ofd.File.Name);
                    //var oldPath = 
      System.IO.Path.GetDirectoryName(ofd.File.Name);

                }

My expected results are "C:\\Users\\Tshililo\\example.csv"我的预期结果是“C:\\Users\\Tshililo\\example.csv”

From the FileDialog.FileName docs :FileDialog.FileName 文档

The file name includes both the file path and the extension.文件名包括文件路径和扩展名。 If no files are selected, this method returns an empty string ("").如果未选择任何文件,此方法将返回一个空字符串 ("")。

OpenFileDialog inherits from the FileDialog class. OpenFileDialog继承自FileDialog类。

Here's how I might use an OpenFileDialog.下面是我如何使用 OpenFileDialog。

  • Calling ShowDialog() will show the dialog and halt your program execution while the user chooses the file.调用ShowDialog()将显示对话框并在用户选择文件时停止程序执行。 After they say OK or Cancel, it resumes.在他们说“确定​​”或“取消”后,它会继续。
  • Inspect the result of ShowDialog() - if it's OK then we can proceed with manipulating the file the user picked.检查ShowDialog()的结果 - 如果没问题,那么我们可以继续操作用户选择的文件。 Most critically the .FileName property holds the full path to the file.最重要的是.FileName属性包含文件的完整路径。
  • If you enabled Multiselect, then the multiple filenames are found in the .FileNames property.如果您启用了多选,则可以在.FileNames属性中找到多个文件名。 .FileName contains the same string as .FileNames[0] , and the order of names in .FileNames is generally the reverse of the order the user chose them. .FileName包含与.FileNames[0]相同的字符串,并且.FileNames中名称的顺序通常与用户选择它们的顺序相反。 If your user is expecting some sort of ordered processing, sort the filenames manually yourself.如果您的用户期望某种有序处理,请自己手动对文件名进行排序。 It's unwise to attach any special meaning to the order of the filenames returned对返回的文件名的顺序附加任何特殊含义是不明智的

//consider declaring this in a USING if you don't have one persistent 
//openfiledialog that you re-use in your app
//i typically keep just one OFD at class level and hide/show it repeatedly
var ofd = new OpenFileDialog();

//set options of ofd before show
ofd.Whatever = whatever;

if(ofd.ShowDialog() == DialogResult.OK){

  //some examples:
  //we could get the full file path the user chose
  var path = ofd.FileName; 

  //or we could delete the file
  File.Delete(ofd.FileName); //2) delete the file or..

  //or we could read the contents of the text file they picked
  var content = File.ReadAllText(ofd.FileName); 

  //or open it as a stream
  var stream = File.OpenStream(ofd.FileName); 

  //or we could get the directory the file is in
  var dir = Path.GetDirectory(ofd.FileName);

  //or we could ponder how deep the directory tree is
  var depth = ofd.FileName.Split(Path.DirectorySeparatorChar.ToString()).Length;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM