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. How to find the Current Path, in Windows Application

How to find the Current Path, in Windows Application

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

    Hi all, i have developed an windows application where in which, i want the current path where the project dll is residing, i mean, here I'm refering to an XML file in the user control like(@"..\\..\xmlfile.xml"); but this path is taking to the application path[Two Up] --after i built this application to an .exe and excute its giving FileNotFound Excpetion.so, i want the current path where the dll is residing, Please help me. Prashanth

    M S 2 Replies Last reply
    0
    • P pashitech

      Hi all, i have developed an windows application where in which, i want the current path where the project dll is residing, i mean, here I'm refering to an XML file in the user control like(@"..\\..\xmlfile.xml"); but this path is taking to the application path[Two Up] --after i built this application to an .exe and excute its giving FileNotFound Excpetion.so, i want the current path where the dll is residing, Please help me. Prashanth

      M Offline
      M Offline
      Muammar
      wrote on last edited by
      #2

      private string CurrentPath = Environment.CurrentDirectory.ToString();

      But watch out when using FileDialoges, it can give you the directory of the file/directory you choose.


      Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

      1 Reply Last reply
      0
      • P pashitech

        Hi all, i have developed an windows application where in which, i want the current path where the project dll is residing, i mean, here I'm refering to an XML file in the user control like(@"..\\..\xmlfile.xml"); but this path is taking to the application path[Two Up] --after i built this application to an .exe and excute its giving FileNotFound Excpetion.so, i want the current path where the dll is residing, Please help me. Prashanth

        S Offline
        S Offline
        Stefan Troschuetz
        wrote on last edited by
        #3

        Take a look at the Application.StartupPath property.


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de

        W N 2 Replies Last reply
        0
        • S Stefan Troschuetz

          Take a look at the Application.StartupPath property.


          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

          www.troschuetz.de

          W Offline
          W Offline
          Wayne Phipps
          wrote on last edited by
          #4

          Please be aware that Application.StartupPath returns the path of the application that started the executable. See: http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx This path is not necessaraly YOUR application. If someone creates a laucncher for your application, if would return the path to their application not yours and also does not include the executable name. The better method would be:

          string path = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
          

          Hope this helps.

          Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog

          1 Reply Last reply
          0
          • S Stefan Troschuetz

            Take a look at the Application.StartupPath property.


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de

            N Offline
            N Offline
            Noman Aftab
            wrote on last edited by
            #5

            Application.StartUpPath will not work for projects having output type as 'class library' use Environment.CurrentDirectory instead

            Pakistani Memon Prime Minister Post Meridian

            W 1 Reply Last reply
            0
            • N Noman Aftab

              Application.StartUpPath will not work for projects having output type as 'class library' use Environment.CurrentDirectory instead

              Pakistani Memon Prime Minister Post Meridian

              W Offline
              W Offline
              Wayne Phipps
              wrote on last edited by
              #6

              A similar problem exists when using Environment.CurrentDirectory as the current directory is not necessarily the folder in which your application resides. You can test this yourself by creating a console application containing the following code:

              		public static void Main(string[] args)
              		{
              			Console.WriteLine("Current Directory: " + Environment.CurrentDirectory.ToString() );
              			Console.WriteLine("Press any key to continue...");
              			Console.ReadKey( true );
              		}
              

              Now if you run the code, it does as you would expect. But if you open your applications folder in explorer then right-click and 'create a shortcut', you can edit the properties of the shortcut and change the 'Start in' parameter to 'C:\'. When you then run the application using the shortcut, the current directory is set to 'C:\' and not that of your application. Please see my previous post for the correct method.

              Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog

              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