Best way to get application folder path

前端 未结 10 2187
無奈伤痛
無奈伤痛 2020-11-22 02:03

I see that there are some ways to get the application folder path:

  1. Application.StartupPath
  2. System.IO.Path.GetDirectoryName( System.
相关标签:
10条回答
  • 2020-11-22 02:32

    AppDomain.CurrentDomain.BaseDirectory is probably the most useful for accessing files whose location is relative to the application install directory.

    In an ASP.NET application, this will be the application root directory, not the bin subfolder - which is probably what you usually want. In a client application, it will be the directory containing the main executable.

    In a VSTO 2005 application, it will be the directory containing the VSTO managed assemblies for your application, not, say, the path to the Excel executable.

    The others may return different directories depending on your environment - for example see @Vimvq1987's answer.

    CodeBase is the place where a file was found and can be a URL beginning with http://. In which case Location will probably be the assembly download cache. CodeBase is not guaranteed to be set for assemblies in the GAC.

    0 讨论(0)
  • 2020-11-22 02:35

    In my experience, the best way is a combination of these.

    1. System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase Will give you the bin folder
    2. Directory.GetCurrentDirectory() Works fine on .Net Core but not .Net and will give you the root directory of the project
    3. System.AppContext.BaseDirectory and AppDomain.CurrentDomain.BaseDirectory Works fine in .Net but not .Net core and will give you the root directory of the project

    In a class library that is supposed to target.Net and .Net core I check which framework is hosting the library and pick one or the other.

    0 讨论(0)
  • 2020-11-22 02:36

    Note that not all of these methods will return the same value. In some cases, they can return the same value, but be careful, their purposes are different:

    Application.StartupPath
    

    returns the StartupPath parameter (can be set when run the application)

    System.IO.Directory.GetCurrentDirectory()
    

    returns the current directory, which may or may not be the folder where the application is located. The same goes for Environment.CurrentDirectory. In case you are using this in a DLL file, it will return the path of where the process is running (this is especially true in ASP.NET).

    0 讨论(0)
  • 2020-11-22 02:39

    If you know to get the root directory:

    string rootPath = Path.GetPathRoot(Application.StartupPath)
    
    0 讨论(0)
  • 2020-11-22 02:41

    Root directory:

    DriveInfo cDrive = new DriveInfo(System.Environment.CurrentDirectory);
    var driverPath = cDrive.RootDirectory;
    
    0 讨论(0)
  • 2020-11-22 02:43

    I started a process from a Windows Service over the Win32 API in the session from the user which is actually logged in (in Task Manager session 1 not 0). In this was we can get to know, which variable is the best.

    For all 7 cases from the question above, the following are the results:

    Path1: C:\Program Files (x86)\MyProgram
    Path2: C:\Program Files (x86)\MyProgram
    Path3: C:\Program Files (x86)\MyProgram\
    Path4: C:\Windows\system32
    Path5: C:\Windows\system32
    Path6: file:\C:\Program Files (x86)\MyProgram
    Path7: C:\Program Files (x86)\MyProgram
    

    Perhaps it's helpful for some of you, doing the same stuff, when you search the best variable for your case.

    0 讨论(0)
提交回复
热议问题