Need help making a File Export function remember the path that the user selects

48 Views Asked by At

I have a function in my program that exports data. The exporting works great, but I'm having trouble with something: When the user clicks Export, they are presented with a folder browser to choose where on their hard drive they would like to export to. The browser launches initially with all the hard drives/folders collapsed like they should be, but I can't figure out how to make it so the program remembers which location the user chose, so that the next time they want to export, it automatically opens to that location instead of once again opening with everything collapsed. I'm just having trouble with the logic, I suppose. Anyone have any tips?

Also, just for clarification, I'm trying to get it to remember the location only for the duration of the session, not like permanently on the register.

Here's my export function so far, if you think that would be helpful:

private void Export(int formatVersion, bool pureXmlDriver)
{
  if (Device != null)
  {
    Utilities.StripShortNameFromLongNames(Device);

    using (var folderBrowser = new FolderBrowserDialog())
    {
      folderBrowser.Description = Resources.SelectExportFolder;

      if (folderBrowser.ShowDialog() == DialogResult.OK)
      {
        string selectedFolder = folderBrowser.SelectedPath;

        try
        {
          Cursor = Cursors.WaitCursor;

          HandleExport(formatVersion, pureXmlDriver, selectedFolder);
        }
        finally
        {
          Cursor = Cursors.Default;
        }
      }
    }
  }
}
1

There are 1 best solutions below

0
On BEST ANSWER

This should do it. You just need a class field to keep the last value in.

public class MyClass
{
    private string selectedPath = "";

    public void Export(int formatVersion, bool pureXmlDriver)
    {
        if (Device != null)
        {
            Utilities.StripShortNameFromLongNames(Device);
            using (var folderBrowser = new FolderBrowserDialog())
           {
               folderBrowser.Description = Resources.SelectExportFolder;
               folderBrowser.SelectedPath = selectedPath;
               if (folderBrowser.ShowDialog() == DialogResult.OK)
               {
                   selectedFolder = folderBrowser.SelectedPath;
                   try
                   {
                       Cursor = Cursors.WaitCursor;
                       HandleExport(formatVersion, pureXmlDriver, selectedFolder);
                   }
                   finally
                   {
                       Cursor = Cursors.Default;
                   }
               }
           }
       }
   }    
}