Search code examples
c#.netwpfwinformsopenfiledialog

Get path from openfile dialog


I open a file in this way and that works fine:

var openFileDialog = new OpenFileDialog;

if (openFileDialog.ShowDialog().GetValueOrDefault())
{
    Browser.FileDoc = File.ReadAllText(openFileDialog.FileName);
}

Now, I want to get the path and pass it to another class. How can I do that?


Solution

  • you can use the below menioned code.

    OpenFileDialog ofd = new OpenFileDialog();
    if (ofd.ShowDialog() == true)
    {
    string filePath = ofd.FileName;
    Class2 c=new Class2(filePath);
    }
    

    Suppose your another Class is Class2 then

    public class Class2
    {
    string Path=String.Empty;
     public Class2(string _Path)
      {
        Path=_Path;
       }
    }