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. OpenFile and SaveFile dialogs change the current directory.

OpenFile and SaveFile dialogs change the current directory.

Scheduled Pinned Locked Moved C#
4 Posts 4 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.
  • S Offline
    S Offline
    Sharpoverride
    wrote on last edited by
    #1

    I wish to know why OpenFile and SaveFile dialogs change the current directory. And if there si a way to prevent this, or shoud I use the following methond : Directory.SetCurrentDirectory( path ); This is what I curently use, and wanted to know if there is some other way. Lazar Mihai

    S D L 3 Replies Last reply
    0
    • S Sharpoverride

      I wish to know why OpenFile and SaveFile dialogs change the current directory. And if there si a way to prevent this, or shoud I use the following methond : Directory.SetCurrentDirectory( path ); This is what I curently use, and wanted to know if there is some other way. Lazar Mihai

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      That's how they are expected to behave. Instead of using CurrentDirectory, you can try using System.AppDomain.CurrentDomain.ApplicationDirectory or something similar. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      • S Sharpoverride

        I wish to know why OpenFile and SaveFile dialogs change the current directory. And if there si a way to prevent this, or shoud I use the following methond : Directory.SetCurrentDirectory( path ); This is what I curently use, and wanted to know if there is some other way. Lazar Mihai

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Your code really shouldn't be depending on the current directory anyway. The general rule-of-thumb in programming is assume nothing!. What this means is you never assume that the file you want to access is in the current directory. Always use fully qualified path names and not just a filename when doing any kind of file access. This also has other benefits. Say you put up a OpenFileDialog that changed the current directory. After the user picks a file, you try and put the current directory back to what it was. But, your code crashes, for whatever reason, before that can happen! The function ends up returning as failed, but not having restored the current directory. Now what? Your next file access is assuming (there's that word again ;)) the current directory is correct when it's not, and your app ends up crashing a second time, then a third, forth, fifth, ... Write your code so it defends itself against itself. Never assume anything is what you expect it to be. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 16:07 Monday 12th September, 2005

        1 Reply Last reply
        0
        • S Sharpoverride

          I wish to know why OpenFile and SaveFile dialogs change the current directory. And if there si a way to prevent this, or shoud I use the following methond : Directory.SetCurrentDirectory( path ); This is what I curently use, and wanted to know if there is some other way. Lazar Mihai

          L Offline
          L Offline
          Large Data File
          wrote on last edited by
          #4

          Use this: System.Windows.Forms.OpenFileDialog dlgOpenFile = new System.Windows.Forms.OpenFileDialog(); ... **dlgOpenFile.RestoreDirectory = true;** DialogResult res = dlgOpenFile.ShowDialog(); But listen to dave's answer, he is totaly right about assuming paths.

          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