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. Go back to old file path after open file dialog box

Go back to old file path after open file dialog box

Scheduled Pinned Locked Moved C#
xmlhelpquestion
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.
  • A Offline
    A Offline
    AhmedMasum
    wrote on last edited by
    #1

    I am having a problem for the following case: I have an xml file in the directory of the myproject.exe file of my project. as a result i m using relative path to open the xml file. I am openig the file using the following code: XmlTextReader xtr = new XmlTextReader("center.xml"); I have a open file dialog box in that project and i read a MSAccess file using that open file dialog box. But after reading the MSAccess file when I try to read the xml file with the same code then exception raised and shows could not find file "path of the MSAccessfile\center.xml" So how can I get back to the directory whre the myproject.exe file located"

    L N 2 Replies Last reply
    0
    • A AhmedMasum

      I am having a problem for the following case: I have an xml file in the directory of the myproject.exe file of my project. as a result i m using relative path to open the xml file. I am openig the file using the following code: XmlTextReader xtr = new XmlTextReader("center.xml"); I have a open file dialog box in that project and i read a MSAccess file using that open file dialog box. But after reading the MSAccess file when I try to read the xml file with the same code then exception raised and shows could not find file "path of the MSAccessfile\center.xml" So how can I get back to the directory whre the myproject.exe file located"

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can get the .exe filename from : Process.MainModule.FileName Then get the directory from your process .exe and set the directory to OpenFileDialog.InitialDirectory.

      A 1 Reply Last reply
      0
      • A AhmedMasum

        I am having a problem for the following case: I have an xml file in the directory of the myproject.exe file of my project. as a result i m using relative path to open the xml file. I am openig the file using the following code: XmlTextReader xtr = new XmlTextReader("center.xml"); I have a open file dialog box in that project and i read a MSAccess file using that open file dialog box. But after reading the MSAccess file when I try to read the xml file with the same code then exception raised and shows could not find file "path of the MSAccessfile\center.xml" So how can I get back to the directory whre the myproject.exe file located"

        N Offline
        N Offline
        Nagy Vilmos
        wrote on last edited by
        #3

        Further to the pevious answer, using relative paths isn't a great idea. If you know for sure where a file is then use the path. The file center.xml should probably be in the runnign directory and not the application directory. Though normally the same, they can be different. The best to use is System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)


        Panic, Chaos, Destruction. My work here is done.

        1 Reply Last reply
        0
        • L Lost User

          You can get the .exe filename from : Process.MainModule.FileName Then get the directory from your process .exe and set the directory to OpenFileDialog.InitialDirectory.

          A Offline
          A Offline
          AhmedMasum
          wrote on last edited by
          #4

          I have done the following code and it solved my problem.

          //using System.Diagnostics;
          try
          {
          Process p = Process.GetCurrentProcess();
          String location = p.MainModule.FileName;
          int start = location.IndexOf(Process.GetCurrentProcess().ProcessName.ToString());
          // MessageBox.Show(location+" "+start.ToString());
          location = location.Remove(start);
          // MessageBox.Show(location);
          xml = location + "center.xml";
          }

          L 1 Reply Last reply
          0
          • A AhmedMasum

            I have done the following code and it solved my problem.

            //using System.Diagnostics;
            try
            {
            Process p = Process.GetCurrentProcess();
            String location = p.MainModule.FileName;
            int start = location.IndexOf(Process.GetCurrentProcess().ProcessName.ToString());
            // MessageBox.Show(location+" "+start.ToString());
            location = location.Remove(start);
            // MessageBox.Show(location);
            xml = location + "center.xml";
            }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            string directoryName = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); string xml = directoryName + @"\center.xml";

            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