How can I get the application's path in a .NET console application?

后端 未结 27 1397
甜味超标
甜味超标 2020-11-21 11:11

How do I find the application\'s path in a console application?

In Windows Forms, I can use Application.StartupPath to find the current path, but this d

相关标签:
27条回答
  • 2020-11-21 11:47

    Assembly.GetEntryAssembly().Location or Assembly.GetExecutingAssembly().Location

    Use in combination with System.IO.Path.GetDirectoryName() to get only the directory.

    The paths from GetEntryAssembly() and GetExecutingAssembly() can be different, even though for most cases the directory will be the same.

    With GetEntryAssembly() you have to be aware that this can return null if the entry module is unmanaged (ie C++ or VB6 executable). In those cases it is possible to use GetModuleFileName from the Win32 API:

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
    
    0 讨论(0)
  • 2020-11-21 11:47

    Another solution is using relative paths pointing to the current path:

    Path.GetFullPath(".")
    
    0 讨论(0)
  • 2020-11-21 11:48

    Probably a bit late but this is worth a mention:

    Environment.GetCommandLineArgs()[0];
    

    Or more correctly to get just the directory path:

    System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
    

    Edit:

    Quite a few people have pointed out that GetCommandLineArgs is not guaranteed to return the program name. See The first word on the command line is the program name only by convention. The article does state that "Although extremely few Windows programs use this quirk (I am not aware of any myself)". So it is possible to 'spoof' GetCommandLineArgs, but we are talking about a console application. Console apps are usually quick and dirty. So this fits in with my KISS philosophy.

    0 讨论(0)
  • 2020-11-21 11:48

    If you are looking for a .NET Core compatible way, use

    System.AppContext.BaseDirectory
    

    This was introduced in .NET Framework 4.6 and .NET Core 1.0 (and .NET Standard 1.3). See: AppContext.BaseDirectory Property.

    According to this page,

    This is the prefered replacement for AppDomain.CurrentDomain.BaseDirectory in .NET Core

    0 讨论(0)
  • 2020-11-21 11:48

    For Console Applications, you can try this:

    System.IO.Directory.GetCurrentDirectory();
    

    Output (on my local machine):

    c:\users\xxxxxxx\documents\visual studio 2012\Projects\ImageHandler\GetDir\bin\Debug

    Or you can try (there's an additional backslash in the end):

    AppDomain.CurrentDomain.BaseDirectory
    

    Output:

    c:\users\xxxxxxx\documents\visual studio 2012\Projects\ImageHandler\GetDir\bin\Debug\

    0 讨论(0)
  • 2020-11-21 11:51

    I didn't see anyone convert the LocalPath provided by .Net Core reflection into a usable System.IO path so here's my version.

    public static string GetApplicationRoot()
    {
       var exePath = new Uri(System.Reflection.
       Assembly.GetExecutingAssembly().CodeBase).LocalPath;
    
       return new FileInfo(exePath).DirectoryName;
           
    }
    

    This will return the full C:\\xxx\\xxx formatted path to where your code is.

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