通过控制台程序打印出每种方法的结果
static void Main(string[] args) { Console.WriteLine("typeof(Program).Assembly.Location: {0}", typeof(Program).Assembly.Location); Console.WriteLine(""); Console.WriteLine("Process.GetCurrentProcess().MainModule.FileName: {0}", Process.GetCurrentProcess().MainModule.FileName); Console.WriteLine(""); Console.WriteLine("Environment.CurrentDirectory: {0}", Environment.CurrentDirectory); Console.WriteLine(""); Console.WriteLine("AppDomain.CurrentDomain.BaseDirectory: {0}", AppDomain.CurrentDomain.BaseDirectory); Console.WriteLine(""); Console.WriteLine("AppDomain.CurrentDomain.SetupInformation.ApplicationBase: {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:UsersAdministratorDocumentsVisual Studio 2015ProjectsTestExecutePathTes
tExecutePathinDebugTestExecutePath.exe
Process.GetCurrentProcess().MainModule.FileName:
C:UsersAdministratorDocumentsVisual Studio 2015ProjectsTestExecutePathTes
tExecutePathinDebugTestExecutePath.exe
Environment.CurrentDirectory:
C:UsersAdministratorDocumentsVisual Studio 2015ProjectsTestExecutePathTes
tExecutePath.StartupinDebug
AppDomain.CurrentDomain.BaseDirectory:
C:UsersAdministratorDocumentsVisual Studio 2015ProjectsTestExecutePathTes
tExecutePathinDebug
AppDomain.CurrentDomain.SetupInformation.ApplicationBase:
C:UsersAdministratorDocumentsVisual Studio 2015ProjectsTestExecutePathTes
tExecutePathinDebug
直接运行结果:(注意不是通过点击调试运行)
typeof(Program).Assembly.Location:
C:UsersAdministratorDocumentsVisual Studio 2015ProjectsTestExecutePathTes
tExecutePathinDebugTestExecutePath.exe
Process.GetCurrentProcess().MainModule.FileName:
C:UsersAdministratorDocumentsVisual Studio 2015ProjectsTestExecutePathTes
tExecutePathinDebugTestExecutePath.exe
Environment.CurrentDirectory:
C:UsersAdministratorDocumentsVisual Studio 2015ProjectsTestExecutePathTes
tExecutePathinDebug
AppDomain.CurrentDomain.BaseDirectory:
C:UsersAdministratorDocumentsVisual Studio 2015ProjectsTestExecutePathTes
tExecutePathinDebug
AppDomain.CurrentDomain.SetupInformation.ApplicationBase:
C:UsersAdministratorDocumentsVisual Studio 2015ProjectsTestExecutePathTes
tExecutePathinDebug