zoukankan      html  css  js  c++  java
  • C#中AppDomain.CurrentDomain.BaseDirectory及各种路径获取方法(转载)

    新建一个.NET Core控制台项目,代码如下:

    using System;
    using System.IO;
    
    namespace NetCorePath
    {
        class Program
        {
            static void Main(string[] args)
            {
                string path = string.Empty;
    
                // 获取程序的基目录。
                // 结果:C:NetCoreDemoNetCorePathNetCorePathinDebug
    etcoreapp3.0
                path = AppDomain.CurrentDomain.BaseDirectory;
                Console.WriteLine($"AppDomain.CurrentDomain.BaseDirectory = {path}");
    
                // 获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。 
                // 结果:C:NetCoreDemoNetCorePathNetCorePathinDebug
    etcoreapp3.0
                path = Environment.CurrentDirectory;
                Console.WriteLine($"Environment.CurrentDirectory = {path}");
    
                // 获取应用程序的当前工作目录,注意工作目录是可以改变的,而不限定在程序所在目录。 
                // 结果:C:NetCoreDemoNetCorePathNetCorePathinDebug
    etcoreapp3.0
                path = Directory.GetCurrentDirectory();
                Console.WriteLine($"Directory.GetCurrentDirectory() = {path}");
    
                // 获取和设置包括该应用程序的目录的名称。 
                // 结果:C:NetCoreDemoNetCorePathNetCorePathinDebug
    etcoreapp3.0
                path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                Console.WriteLine($"AppDomain.CurrentDomain.SetupInformation.ApplicationBase = {path}");
    
                Console.WriteLine("Press any key to end...");
                Console.ReadKey();
            }
        }
    }

    其中:

    我们平时用的最多的应该是AppDomain.CurrentDomain.BaseDirectory属性,它可以返回各种.NET项目的当前运行路径。

    其次在ASP.NET Core项目中,我们有时候会用到Environment.CurrentDirectory,这个属性在ASP.NET Core项目发布前(在Visual Studio中运行ASP.NET Core项目)和发布后,返回的路径会有所不同。当ASP.NET Core项目发布后,AppDomain.CurrentDomain.BaseDirectory属性和Environment.CurrentDirectory属性会得到相同的文件夹。但在ASP.NET Core项目发布前(在Visual Studio中运行ASP.NET Core项目),AppDomain.CurrentDomain.BaseDirectory属性和Environment.CurrentDirectory属性会得到不同的文件夹路径。

    原文链接

  • 相关阅读:
    图床_shell命令grep/egrep
    图床_shell命令vi/vim
    图床_shell命令tr
    图床_shell命令wc
    图床_shell命令vimdiff
    图床_shell命令diff
    图床_shell命令rev
    图床_shell命令cut
    图床_shell命令tail
    图床_shell命令head
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/12106797.html
Copyright © 2011-2022 走看看