Search code examples
c#winformsopenfiledialog

C# FilePath Help


I use OpenFileDialog to search for a specific file. When the user chooses the file, I want to store that path in a variable. However, these doesn't seem to be an option for this within OpenFileDialog?

Does anybody know how to do this?

Thanks.

Edit: This is Winforms, and I don't want to save the path inclusive of the filename, just the location where the file is.


Solution

  • This will retrieve your path based on the FileName property of the OpenFileDialog.

    String path = System.IO.Path.GetDirectoryName(OpenFileDialog.FileName);