zoukankan      html  css  js  c++  java
  • C#获取程序执行路径的几种方法

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

    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

    点击下载代码

  • 相关阅读:
    GC算法 垃圾收集器
    JVM内存结构
    java类的加载机制
    Spring Boot:Web 综合开发
    构建微服务:Spring boot 入门篇
    Spring Boot:Spring Boot 中 Redis 的使用
    Spring Boot:Thymeleaf 使用详解
    Android ActionBar完全解析,使用官方推荐的最佳导航栏(下)
    用ActionBar的ActionProvider的时候报错:cannot be cast to android.view.ActionProvider
    百度图片API
  • 原文地址:https://www.cnblogs.com/jefflee/p/5868484.html
Copyright © 2011-2022 走看看