Search code examples
c#visual-studioxlsxsavefiledialog

Where to find selected filepath of SaveFileDialog (C#, Visual Studio)


I'm trying to export my dataTable as an Excel file. I can do this from (https://stackoverflow.com/a/13973274), but I'd like the user to be able to input the location the file is saved to. It seems like the SaveFileDialog class would work well.

My issue stems from the fact that I can't figure out where to find the filepath that has been specified by the user after hitting "save". I need that filepath to input into the ExportToExcel(ExcelFilePath) when I actually do the export of my dataTable.

SaveFileDialog Class

Sorry if I'm unclear, this is my first time asking a question here.


Solution

  • As you've noted, the SaveFileDialog class is documented here.

    If we look at the top of that page, it shows the inheritance hierarchy:

    Inheritance → ObjectMarshalByRefObjectComponentCommonDialogFileDialog → SaveFileDialog

    Now if we follow the link to the FileDialog class and go to Properties, we see:

    FileDialog.FileName Property
    Property Value
    String
    The file name selected in the file dialog box. The default value is an empty string ("").

    Remarks
    The file name includes both the file path and the extension. If no files are selected, this method returns an empty string ("").