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 get the execution folder if the file executed is in another folder?

How to get the execution folder if the file executed is in another folder?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
4 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.
  • T Offline
    T Offline
    Ted On The Net
    wrote on last edited by
    #1

    Hi, I want to make some app startup from the commandline no matter in which folder I am. So I was thinking of putting my executable in the c:\Windows folder. now if I start the exe from "C:\program files" I want to know this path instead of the Application.StartupPath (which returns "C:\Windows") Can anybody tell me how to get this path? Thanks. Ted

    - Life would be so much easier if I had the source code! - If C# had true garbage collection, most applications would delete themselves upon execution ;)

    T R K 3 Replies Last reply
    0
    • T Ted On The Net

      Hi, I want to make some app startup from the commandline no matter in which folder I am. So I was thinking of putting my executable in the c:\Windows folder. now if I start the exe from "C:\program files" I want to know this path instead of the Application.StartupPath (which returns "C:\Windows") Can anybody tell me how to get this path? Thanks. Ted

      - Life would be so much easier if I had the source code! - If C# had true garbage collection, most applications would delete themselves upon execution ;)

      T Offline
      T Offline
      Ted On The Net
      wrote on last edited by
      #2

      found it, this does the trick:

      Directory.GetCurrentDirectory();

      - Life would be so much easier if I had the source code! - If C# had true garbage collection, most applications would delete themselves upon execution ;)

      1 Reply Last reply
      0
      • T Ted On The Net

        Hi, I want to make some app startup from the commandline no matter in which folder I am. So I was thinking of putting my executable in the c:\Windows folder. now if I start the exe from "C:\program files" I want to know this path instead of the Application.StartupPath (which returns "C:\Windows") Can anybody tell me how to get this path? Thanks. Ted

        - Life would be so much easier if I had the source code! - If C# had true garbage collection, most applications would delete themselves upon execution ;)

        R Offline
        R Offline
        R Giskard Reventlov
        wrote on last edited by
        #3

        If I have understood your requirement correctly you could create an environment variable to give you accees to your application form anywhere. On the Advanced tab of System Properties click on the Environment Variables button. Add the path to your app to the PATH variable and you will be able to start it from anywhere.

        me, me, me "The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!" Larry Niven

        1 Reply Last reply
        0
        • T Ted On The Net

          Hi, I want to make some app startup from the commandline no matter in which folder I am. So I was thinking of putting my executable in the c:\Windows folder. now if I start the exe from "C:\program files" I want to know this path instead of the Application.StartupPath (which returns "C:\Windows") Can anybody tell me how to get this path? Thanks. Ted

          - Life would be so much easier if I had the source code! - If C# had true garbage collection, most applications would delete themselves upon execution ;)

          K Offline
          K Offline
          KChandos
          wrote on last edited by
          #4

          You can pick up the location of many different "special" folders. This will get you the Program Files folder:

          string strProgFilesDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

          If, however, you really meant that you wanted the name of the directory where the assembly was located, then you should use:

          using System.IO;

          .
          .
          .

          string strMyHomeDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

          As an aside, rather than adding a program to the Windows folder, you should create a folder elsewhere (I have one called Tools) and add that folder to your environment Path. Perhaps it's just me but I don't like cluttering up system file locations.

          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