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

后端 未结 27 1402
甜味超标
甜味超标 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 12:00

    I have used this code and get the solution.

    AppDomain.CurrentDomain.BaseDirectory
    
    0 讨论(0)
  • 2020-11-21 12:00

    Following line will give you an application path:

    var applicationPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
    

    Above solution is working properly in the following situations:

    • simple app
    • in another domain where Assembly.GetEntryAssembly() would return null
    • DLL is loaded from Embedded resources as a byte array and loaded to AppDomain as Assembly.Load(byteArrayOfEmbeddedDll)
    • with Mono's mkbundle bundles (no other methods work)
    0 讨论(0)
  • 2020-11-21 12:01
    AppDomain.CurrentDomain.BaseDirectory
    

    Will resolve the issue to refer the 3rd party reference files with installation packages.

    0 讨论(0)
  • 2020-11-21 12:01

    None of these methods work in special cases like using a symbolic link to the exe, they will return the location of the link not the actual exe.

    So can use QueryFullProcessImageName to get around that:

    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Diagnostics;
    
    internal static class NativeMethods
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern bool QueryFullProcessImageName([In]IntPtr hProcess, [In]int dwFlags, [Out]StringBuilder lpExeName, ref int lpdwSize);
    
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern IntPtr OpenProcess(
            UInt32 dwDesiredAccess,
            [MarshalAs(UnmanagedType.Bool)]
            Boolean bInheritHandle,
            Int32 dwProcessId
        );
    }
    
    public static class utils
    {
    
        private const UInt32 PROCESS_QUERY_INFORMATION = 0x400;
        private const UInt32 PROCESS_VM_READ = 0x010;
    
        public static string getfolder()
        {
            Int32 pid = Process.GetCurrentProcess().Id;
            int capacity = 2000;
            StringBuilder sb = new StringBuilder(capacity);
            IntPtr proc;
    
            if ((proc = NativeMethods.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pid)) == IntPtr.Zero)
                return "";
    
            NativeMethods.QueryFullProcessImageName(proc, 0, sb, ref capacity);
    
            string fullPath = sb.ToString(0, capacity);
    
            return Path.GetDirectoryName(fullPath) + @"\";
        }
    }
    
    0 讨论(0)
  • 2020-11-21 12:02

    You can use the following code to get the current application directory.

    AppDomain.CurrentDomain.BaseDirectory
    
    0 讨论(0)
  • 2020-11-21 12:02

    I mean, why not a p/invoke method?

        using System;
        using System.IO;
        using System.Runtime.InteropServices;
        using System.Text;
        public class AppInfo
        {
                [DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
                private static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
                private static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
                public static string StartupPath
                {
                    get
                    {
                        StringBuilder stringBuilder = new StringBuilder(260);
                        GetModuleFileName(NullHandleRef, stringBuilder, stringBuilder.Capacity);
                        return Path.GetDirectoryName(stringBuilder.ToString());
                    }
                }
        }
    

    You would use it just like the Application.StartupPath:

        Console.WriteLine("The path to this executable is: " + AppInfo.StartupPath + "\\" + System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe");
    
    0 讨论(0)
提交回复
热议问题