Best way to get application folder path

2018-12-31 07:31发布

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

  1. Application.StartupPath
  2. System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location)
  3. AppDomain.CurrentDomain.BaseDirectory
  4. System.IO.Directory.GetCurrentDirectory()
  5. Environment.CurrentDirectory
  6. System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
  7. System.IO.Path.GetDirectory(Application.ExecutablePath)

What is the best way depending on the situation?

标签: c# .net
9条回答
余欢
2楼-- · 2018-12-31 07:43

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).

查看更多
骚的不知所云
3楼-- · 2018-12-31 07:43

I have used this one successfully

System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)

It works even inside linqpad.

查看更多
步步皆殇っ
4楼-- · 2018-12-31 07:50

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.

查看更多
弹指情弦暗扣
5楼-- · 2018-12-31 07:52

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.

查看更多
呛了眼睛熬了心
6楼-- · 2018-12-31 07:56

As Example :

  1. Button : [ Name - 'btn_OpenFile']
  2. I have 'abc.exe' which i want to open.
  3. File Path : C:\Users\Admin\Documents\Visual Studio 2015\Projects\MyProject\MyProject\abc.exe.

So, I'll do the Following :

  1. In the Button Click event : btn_OpenFile_Click()

Process.Start(@Environment.CurrentDirectory+"\\..\\..\\abc.exe");

Note : "Environment.CurrentDirectory" return this path : "C:\\Users\\Admin\\Documents\\Visual Studio 2015\\Projects\\MyProject\\MyProject\\bin\\Debug"

So, By Putting "\\.." this, you can go to higher directory.

查看更多
萌妹纸的霸气范
7楼-- · 2018-12-31 07:57

this one "System.IO.Path.GetDirectory(Application.ExecutablePath)" changed to System.IO.Path.GetDirectoryName(Application.ExecutablePath)

查看更多
登录 后发表回答