Setting the initial directory of an SaveFileDialog?

前端 未结 14 2084
一个人的身影
一个人的身影 2020-12-01 10:05

I\'d like a SaveFileDialog with the following behavior:

  • The first time you open it, it goes to \"My Documents\".

  • Afterwards, it goes to the

相关标签:
14条回答
  • 2020-12-01 10:44

    If you use forward slash anywhere in your path, InitialDirectory does not work. Make sure they are converted to back slashes

    0 讨论(0)
  • 2020-12-01 10:45

    You need to set the RestoreDirectory to true as well as the InitialDirectory property.

    0 讨论(0)
  • 2020-12-01 10:49

    None of the provided solutions worked for me sadly.

    In addition to the OP specifications, I wanted the program to remember the last save location between runs. For this, in the Visual Studios Solution Explorer under ProjectName -> Properties -> Settings.settings, I setup the following property:

    Because I am keeping the SaveFileDialog around for the duration of the program's running, I instantiate at the start. Then in the Command for bringing up the dialog:

    if (string.IsNullOrEmpty(Settings.Default.PreviousPath))
    {
        this.saveDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    }
    else
    {
        this.saveDialog.InitialDirectory = Settings.Default.PreviousPath;
    }
    
    this.saveDialog.FileName = "Default_File_Name";
    
    bool result = this.saveDialog.ShowDialog() ?? false;
    
    if (result)
    {
        Settings.Default.PreviousPath = Path.GetDirectoryName(this.saveDialog.FileName);
        Settings.Default.Save();
    
        // Code writing to the new file...
    }
    

    This gives the behavior:

    • First time the program is run and dialog is opened, it navigates to "My Documents".
    • Consecutive runs and dialog opened:
      • If saved on a previous open, it navigates to previous save location.
      • If not saved on a previous open, navigates to "My Documents".
    0 讨论(0)
  • 2020-12-01 10:50

    use the saveFileDialog.InitialDirectory property as follows:

    Stream stream;
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    
    saveFileDialog.Filter = "dat files (*.dat)|*.dat|All files (*.*)|*.*";
    saveFileDialog.FilterIndex = 1;
    saveFileDialog.RestoreDirectory = true;
    saveFileDialog.InitialDirectory = @"D:\Data\...\foldername\";
    saveFileDialog.ShowDialog();
    
    if (!saveFileDialog.FileName.Equals(string.Empty))
        Console.WriteLine(saveFileDialog.FileName);
    
    0 讨论(0)
  • 2020-12-01 10:53

    I found that setting InitialDirectory to null first works around user history.

        OpenFileDialog dlgOpen = new OpenFileDialog();
        dlgOpen.InitialDirectory = null;
        dlgOpen.InitialDirectory = @"c:\user\MyPath";
    
    0 讨论(0)
  • 2020-12-01 10:54

    I did some testing with .NET 2.0 and it seems if I set FileName to anything other than a literal string it doesn't work. When I use a method or accesstor to set the property the initial directory is ignored.

    0 讨论(0)
提交回复
热议问题