Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Default Dialog Directories

Default Dialog Directories

Scheduled Pinned Locked Moved C#
questionwpf
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PhilDanger
    wrote on last edited by
    #1

    Hey everyone, This may be a very simple (hopefully it is) question and answer, but I'm having trouble figuring this one out. I want a SaveFileDialog and an OpenFileDialog to start in a chosen directory. Seems simple enough, I'll just create the directory in case it doesn't exist, and then set the InitialDirectory property of the dialog -- but for some reason it's still opening in the last directory that I had browsed to in a previous dialog. Here's the code:

            private const string ATF_FILTER = "AOI Template Files (*.atf)|*.atf";
    
            private const string C_SAVE_DIRECTORY = @"./Saved Templates";
    
            //Save Template
            private void Save_Click(object sender, EventArgs e)
            {
                Directory.CreateDirectory(C_SAVE_DIRECTORY);
                SaveFileDialog diag = new SaveFileDialog();
                diag.InitialDirectory = C_SAVE_DIRECTORY;
                diag.DefaultExt = ".atf";
                diag.Filter = ATF_FILTER;
                if (diag.ShowDialog() == DialogResult.OK)
                {
                    DrawBox.SaveTemplate(diag.FileName);
                }
            }
    

    Any idea what could be going wrong? Thanks, Phil

    L L 2 Replies Last reply
    0
    • P PhilDanger

      Hey everyone, This may be a very simple (hopefully it is) question and answer, but I'm having trouble figuring this one out. I want a SaveFileDialog and an OpenFileDialog to start in a chosen directory. Seems simple enough, I'll just create the directory in case it doesn't exist, and then set the InitialDirectory property of the dialog -- but for some reason it's still opening in the last directory that I had browsed to in a previous dialog. Here's the code:

              private const string ATF_FILTER = "AOI Template Files (*.atf)|*.atf";
      
              private const string C_SAVE_DIRECTORY = @"./Saved Templates";
      
              //Save Template
              private void Save_Click(object sender, EventArgs e)
              {
                  Directory.CreateDirectory(C_SAVE_DIRECTORY);
                  SaveFileDialog diag = new SaveFileDialog();
                  diag.InitialDirectory = C_SAVE_DIRECTORY;
                  diag.DefaultExt = ".atf";
                  diag.Filter = ATF_FILTER;
                  if (diag.ShowDialog() == DialogResult.OK)
                  {
                      DrawBox.SaveTemplate(diag.FileName);
                  }
              }
      

      Any idea what could be going wrong? Thanks, Phil

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      PhilDanger wrote:

      Any idea what could be going wrong?

      Relative path perhaps? As someone pointed out in the last few days, best practice is to use absolute paths.

      P 1 Reply Last reply
      0
      • P PhilDanger

        Hey everyone, This may be a very simple (hopefully it is) question and answer, but I'm having trouble figuring this one out. I want a SaveFileDialog and an OpenFileDialog to start in a chosen directory. Seems simple enough, I'll just create the directory in case it doesn't exist, and then set the InitialDirectory property of the dialog -- but for some reason it's still opening in the last directory that I had browsed to in a previous dialog. Here's the code:

                private const string ATF_FILTER = "AOI Template Files (*.atf)|*.atf";
        
                private const string C_SAVE_DIRECTORY = @"./Saved Templates";
        
                //Save Template
                private void Save_Click(object sender, EventArgs e)
                {
                    Directory.CreateDirectory(C_SAVE_DIRECTORY);
                    SaveFileDialog diag = new SaveFileDialog();
                    diag.InitialDirectory = C_SAVE_DIRECTORY;
                    diag.DefaultExt = ".atf";
                    diag.Filter = ATF_FILTER;
                    if (diag.ShowDialog() == DialogResult.OK)
                    {
                        DrawBox.SaveTemplate(diag.FileName);
                    }
                }
        

        Any idea what could be going wrong? Thanks, Phil

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, it depends on FileDialog.RestoreDirectory which by default is false, but it seems you want it to be true. :)

        Luc Pattyn [My Articles] [Forum Guidelines]

        1 Reply Last reply
        0
        • L led mike

          PhilDanger wrote:

          Any idea what could be going wrong?

          Relative path perhaps? As someone pointed out in the last few days, best practice is to use absolute paths.

          P Offline
          P Offline
          PhilDanger
          wrote on last edited by
          #4

          Turns out it works correctly when I switch it from a forward slash to a backslash: private const string C_SAVE_DIRECTORY = @".\Saved Templates"; //works! Could you elaborate a bit on the benefits of using an absolute path over a relative path? My intuition and previous web experience tells me that using an absolute path would not be the best solution, but perhaps times have changed. Thanks, Phil

          L 1 Reply Last reply
          0
          • P PhilDanger

            Turns out it works correctly when I switch it from a forward slash to a backslash: private const string C_SAVE_DIRECTORY = @".\Saved Templates"; //works! Could you elaborate a bit on the benefits of using an absolute path over a relative path? My intuition and previous web experience tells me that using an absolute path would not be the best solution, but perhaps times have changed. Thanks, Phil

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            PhilDanger wrote:

            benefits of using an absolute path over a relative path?

            Well they are "absolute". Relative is subject to the Current Directory so it's only as good as a guarantee that it hasn't changed or is not something other than what you expect it to be.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups