简体   繁体   English

从打开的文件对话框获取文件路径

[英]Getting File path from a open file dialog

I want to make a button that 我想做一个按钮

  • opens a file from some location in file system, 从文件系统中的某个位置打开文件,
  • gets its file path, 得到它的文件路径,
  • pass the file path like an argument to a method 将文件路径像参数一样传递给方法
  • open that file and do something with it. 打开该文件并对其进行处理。

I've made a button like this: 我做了一个像这样的按钮:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();

            fDialog.Title = "Open XML file";
            fDialog.Filter = "XML files|*.config";
            fDialog.InitialDirectory = @"C:\";
            fDialog.ShowDialog();
        }

I already made a method that reads from hard-coded location, but can someone help me about that file path part variable? 我已经制作了一种从硬编码位置读取的方法,但是有人可以帮助我了解该文件路径部分变量吗?

Method reads file with XmlTextReader like this: 方法使用XmlTextReader读取文件,如下所示:

private void ReadAdvancedConfigFile()
        {
            XElement root = null;
            root = XElement.Load(new XmlTextReader(@"C:\Users\nemanja.mosorinski\Downloads\__Research-master\__Research-master\SEDMSVSPackage\VisualStudioPackage\AppRes\ConfigFiles\Unity.config"));
        }

So basically I want to put new file path for some file founded by OpenFileDialog in root variable. 因此,基本上,我想将由OpenFileDialog创建的某些文件的新文件路径放在根变量中。

Change this line: 更改此行:

fDialog.ShowDialog();

To: 至:

bool? control = fDialog.ShowDialog();
if(control.Value)
{
   var filePath = fDialog.FileName;
   ReadAdvancedConfigFile(filePath)
}

Also you should change the method signature 另外你应该改变方法签名

private void ReadAdvancedConfigFile(string path)

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

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