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

后端 未结 27 1401
甜味超标
甜味超标 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:53

    There are many ways to get executable path, which one we should use it depends on our needs here is a link which discuss different methods.

    Different ways to get Application Executable Path

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

    For anyone interested in asp.net web apps. Here are my results of 3 different methods

    protected void Application_Start(object sender, EventArgs e)
    {
      string p1 = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
      string p2 = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
      string p3 = this.Server.MapPath("");
      Console.WriteLine("p1 = " + p1);
      Console.WriteLine("p2 = " + p2);
      Console.WriteLine("p3 = " + p3);
    }
    

    result

    p1 = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\a897dd66\ec73ff95\assembly\dl3\ff65202d\29daade3_5e84cc01
    p2 = C:\inetpub\SBSPortal_staging\
    p3 = C:\inetpub\SBSPortal_staging
    

    the app is physically running from "C:\inetpub\SBSPortal_staging", so the first solution is definitely not appropriate for web apps.

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

    System.Reflection.Assembly.GetExecutingAssembly().Location1

    Combine that with System.IO.Path.GetDirectoryName if all you want is the directory.

    1As per Mr.Mindor's comment:
    System.Reflection.Assembly.GetExecutingAssembly().Location returns where the executing assembly is currently located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory. System.Reflection.Assembly.GetExecutingAssembly().CodeBase will return the 'permanent' path of the assembly.

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

    You may be looking to do this:

    System.IO.Path.GetDirectoryName(
        System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
    
    0 讨论(0)
  • 2020-11-21 11:57

    Here is a reliable solution that works with 32bit and 64bit applications.

    Add these references:

    using System.Diagnostics;

    using System.Management;

    Add this method to your project:

    public static string GetProcessPath(int processId)
    {
        string MethodResult = "";
        try
        {
            string Query = "SELECT ExecutablePath FROM Win32_Process WHERE ProcessId = " + processId;
    
            using (ManagementObjectSearcher mos = new ManagementObjectSearcher(Query))
            {
                using (ManagementObjectCollection moc = mos.Get())
                {
                    string ExecutablePath = (from mo in moc.Cast<ManagementObject>() select mo["ExecutablePath"]).First().ToString();
    
                    MethodResult = ExecutablePath;
    
                }
    
            }
    
        }
        catch //(Exception ex)
        {
            //ex.HandleException();
        }
        return MethodResult;
    }
    

    Now use it like so:

    int RootProcessId = Process.GetCurrentProcess().Id;
    
    GetProcessPath(RootProcessId);
    

    Notice that if you know the id of the process, then this method will return the corresponding ExecutePath.

    Extra, for those interested:

    Process.GetProcesses() 
    

    ...will give you an array of all the currently running processes, and...

    Process.GetCurrentProcess()
    

    ...will give you the current process, along with their information e.g. Id, etc. and also limited control e.g. Kill, etc.*

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

    You have two options for finding the directory of the application, which you choose will depend on your purpose.

    // to get the location the assembly is executing from
    //(not necessarily where the it normally resides on disk)
    // in the case of the using shadow copies, for instance in NUnit tests, 
    // this will be in a temp directory.
    string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
    
    //To get the location the assembly normally resides on disk or the install directory
    string path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
    
    //once you have the path you get the directory with:
    var directory = System.IO.Path.GetDirectoryName(path);
    
    0 讨论(0)
提交回复
热议问题