C#获取程序执行路径的几种方法

通过控制台程序打印出每种方法的结果

static void Main(string[] args)
        {
            
            Console.WriteLine("typeof(Program).Assembly.Location:\r\n{0}", typeof(Program).Assembly.Location);
            Console.WriteLine("");

            Console.WriteLine("Process.GetCurrentProcess().MainModule.FileName:\r\n{0}", Process.GetCurrentProcess().MainModule.FileName);
            Console.WriteLine("");

            Console.WriteLine("Environment.CurrentDirectory:\r\n{0}", Environment.CurrentDirectory);
            Console.WriteLine("");

            Console.WriteLine("AppDomain.CurrentDomain.BaseDirectory:\r\n{0}", AppDomain.CurrentDomain.BaseDirectory);
            Console.WriteLine("");


            Console.WriteLine("AppDomain.CurrentDomain.SetupInformation.ApplicationBase:\r\n{0}", AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
            Console.WriteLine("");



            Console.Read();
        }

通过另一个程序启动的代码

        static void Main(string[] args)
        {
            String path = "..\\..\\..\\TestExecutePath\\bin\\Debug\\TestExecutePath.exe";
            Console.WriteLine("准备启动:"+path);

            try
            {
                Process.Start(path);
                Console.WriteLine("启动成功");
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.Read();
        }

通过其他程序启动的运行结果:

typeof(Program).Assembly.Location:
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestExecutePath\Tes
tExecutePath\bin\Debug\TestExecutePath.exe

Process.GetCurrentProcess().MainModule.FileName:
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestExecutePath\Tes
tExecutePath\bin\Debug\TestExecutePath.exe

Environment.CurrentDirectory:
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestExecutePath\Tes
tExecutePath.Startup\bin\Debug

AppDomain.CurrentDomain.BaseDirectory:
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestExecutePath\Tes
tExecutePath\bin\Debug\

AppDomain.CurrentDomain.SetupInformation.ApplicationBase:
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestExecutePath\Tes
tExecutePath\bin\Debug\

直接运行结果:(注意不是通过点击调试运行)

typeof(Program).Assembly.Location:
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestExecutePath\Tes
tExecutePath\bin\Debug\TestExecutePath.exe

Process.GetCurrentProcess().MainModule.FileName:
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestExecutePath\Tes
tExecutePath\bin\Debug\TestExecutePath.exe

Environment.CurrentDirectory:
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestExecutePath\Tes
tExecutePath\bin\Debug

AppDomain.CurrentDomain.BaseDirectory:
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestExecutePath\Tes
tExecutePath\bin\Debug\

AppDomain.CurrentDomain.SetupInformation.ApplicationBase:
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\TestExecutePath\Tes
tExecutePath\bin\Debug\

点击下载代码

posted @ 2018-12-12 14:39  Jeff-Lee  阅读(6172)  评论(0编辑  收藏  举报
2047179505-asdf123456/*-