Question

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:

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.

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top