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 Application running path

Default Application running path

Scheduled Pinned Locked Moved C#
xmlhelptutorial
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
    StephenMcAllister
    wrote on last edited by
    #1

    Hi, I have an application which runs well until an open dialog box is accessed by the user. What happens is that is appears when the user selects the file to open it changes the default directory for the application and the application tries to access a file in the new directory rether than the application default directory. For example. The default directory is C:\Program Files\App The file path the user selects is C:Temp I have an .xml file in C:\Program Files\App which is accessed fine before the open dialog but after the open dialog the program crashes because it is looking for the .xml file in the C:Temp directory and not the C:\Program Files\App directory. What I want to do is force the program to always look for this file in the C:\Program Files\App directory without hard coding the path. Any ideas and help are appreciated. Regards Stephen McAllister

    D D D 3 Replies Last reply
    0
    • S StephenMcAllister

      Hi, I have an application which runs well until an open dialog box is accessed by the user. What happens is that is appears when the user selects the file to open it changes the default directory for the application and the application tries to access a file in the new directory rether than the application default directory. For example. The default directory is C:\Program Files\App The file path the user selects is C:Temp I have an .xml file in C:\Program Files\App which is accessed fine before the open dialog but after the open dialog the program crashes because it is looking for the .xml file in the C:Temp directory and not the C:\Program Files\App directory. What I want to do is force the program to always look for this file in the C:\Program Files\App directory without hard coding the path. Any ideas and help are appreciated. Regards Stephen McAllister

      D Offline
      D Offline
      DougW48
      wrote on last edited by
      #2

      Before calling the file picker, store your current directory, then set it again afterwards. string sCurrentDirectory = System.IO.Directory.GetCurrentDirectory(); // DO YOUR FILE PICKING System.IO.Directory.SetCurrentDirectory( sCurrentDirectory );

      1 Reply Last reply
      0
      • S StephenMcAllister

        Hi, I have an application which runs well until an open dialog box is accessed by the user. What happens is that is appears when the user selects the file to open it changes the default directory for the application and the application tries to access a file in the new directory rether than the application default directory. For example. The default directory is C:\Program Files\App The file path the user selects is C:Temp I have an .xml file in C:\Program Files\App which is accessed fine before the open dialog but after the open dialog the program crashes because it is looking for the .xml file in the C:Temp directory and not the C:\Program Files\App directory. What I want to do is force the program to always look for this file in the C:\Program Files\App directory without hard coding the path. Any ideas and help are appreciated. Regards Stephen McAllister

        D Offline
        D Offline
        Dennis C Dietrich
        wrote on last edited by
        #3

        StephenMcAllister wrote: I have an .xml file in C:\Program Files\App which is accessed fine before the open dialog but after the open dialog the program crashes because it is looking for the .xml file in the C:Temp directory and not the C:\Program Files\App directory. Assuming that C:\Program Files\App is the path where your executable resides you can use the Application.StartupPath[^] property. Best regards Dennis

        1 Reply Last reply
        0
        • S StephenMcAllister

          Hi, I have an application which runs well until an open dialog box is accessed by the user. What happens is that is appears when the user selects the file to open it changes the default directory for the application and the application tries to access a file in the new directory rether than the application default directory. For example. The default directory is C:\Program Files\App The file path the user selects is C:Temp I have an .xml file in C:\Program Files\App which is accessed fine before the open dialog but after the open dialog the program crashes because it is looking for the .xml file in the C:Temp directory and not the C:\Program Files\App directory. What I want to do is force the program to always look for this file in the C:\Program Files\App directory without hard coding the path. Any ideas and help are appreciated. Regards Stephen McAllister

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

          Follow Dennis' link to the Application.StartupPath(). The problem your having is that your assuming that the current directory will never change when you app is open. This is simply not the case. As you've found out, it can change at any time. When you code file paths, NEVER use just a filename. ALWAYS prefix your filepaths with a complete path specification. You can use the Application.StartupPath() to return the directory path that your .EXE was launched from. Then use the Path class' Combine() method to build a full path specification to your .XML file. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          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