开发者

C# Executable Executing directory

What is the best method of getting the path the C# executable is running from?

I need to use it for temp folders etc and currently I'm using:

Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)

But I then need to do a:

.Replace("file:\\", string.Empty)

as this gives me a URI type path (i.e. has file:\ at the start) which is unusable with some other parts of my开发者_JAVA技巧 code.


Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)


Not an accurate answer to your question, however I would suggest to have a look at:

Path.GetTempFileName()

http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename.aspx


Try with Environment.CurrentDirectory This returns the folder of your currently working directory.


For temp folders etc. you should NOT use the executing directory for security reasons... MS has something built-in for this:

You can use ApplicationData (for roaming users) or LocalApplicationData (for non-roaming users) or CommonApplicationData (for non-user-related things) from Environment.SpecialFolder - anything (files+folders) you create within those folders is already setup with the needed permissions/rights for the user running you app - nobody else (except perhaps Administrator) can go there... to make it even more secure you could encrypt data you put there...

see http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx


Try:

Application.StartupPath

It should return the path without the executable filename.


If the assembly has been shadow copied then Assembly.Location will point to the shadow copy of the dll.

If you want to know the location of the build output directory use Assembly.CodeBase, i.e.:

Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath)

Useful if you want to access other resources from the directory of the assembly for example when running test suite.


  • .NET Core 3.0 introduced IHostEnvironment.ContentRootPath.
  • .NET Core 2.1 introduced IHostingEnvironment.ContentRootPath (obsolete since Core 3.0).

Personally I find that these are what I generally need: the place where included content is located.

I can confirm that IHostEnvironment.ContentRootPath also returns the desired result when running as a unit test. (Tested with xUnit, but the test framework should not matter.)


For self-contained single file applications (available since .NET Core 3.0) you should use System.AppContext.BaseDirectory. The assembly location will be empty so you can't rely on it.

Example

using System.Reflection;

Console.WriteLine($"AppContext.BaseDirectory: {AppContext.BaseDirectory}");
Console.WriteLine($"Assembly.Location: {Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}");

Console.ReadLine();

Output (Not published):

AppContext.BaseDirectory: C:\[..]\bin\Debug\net6.0-windows\win-x64\
Assembly.Location: C:\[..]\bin\Debug\net6.0-windows\win-x64

Output (Published / Single File):

AppContext.BaseDirectory: C:\[..]\bin\Debug\net6.0-windows\win-x64\publish\
Assembly.Location:


string executableLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string txtfile = Path.Combine(executableLocation, "example.txt");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜