--  .net 获取程序目录,进程目录,模块目录,当前目录的办法及跨win版本,linux上等特殊情况
                                    
【官网】:无
                                
应用场景
不管.net framework处理各windows版本,还是.net core在处理windows,linux不同环境下的目录获取。我们都需要一种方式正确的获取程序或进程所在的目录。基础资源
无
                                使用须知
需要根据项目的实际情况选择。另外应该提早封装测试好,避免发布时出现各种问
配置步骤
A)各种获取当前目录的方式.
	// 获取程序的基目录。
System.AppDomain.CurrentDomain.BaseDirectory
// 获取模块的完整路径,包含文件名
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
// 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
System.Environment.CurrentDirectory
// 获取应用程序的当前工作目录,注意工作目录是可以改变的,而不限定在程序所在目录。
System.IO.Directory.GetCurrentDirectory()
// 获取和设置包括该应用程序的目录的名称。
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
// 获取启动了应用程序的可执行文件的路径 (注:只能在winform 程序中使用)。
System.Windows.Forms.Application.StartupPath    
	
// 获取启动了应用程序的可执行文件的路径及文件名 (注:只能在winform 程序中使用)
System.Windows.Forms.Application.ExecutablePath 
	
B).net framework跨windows版本(windows ce, windows xp,win7,win10等)获取目录的写法.
	public class Configs
{
    private string m_CurrentPath;
    private string Platform
    {
        get
        {
            return Environment.OSVersion.Platform.ToString();
        }}
        public string CurrentPath{
        get
    }
    if(Platform.Equals(“WinCE”))
    {
        m_CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
    }
    else if(Platform.Equals(“Win32NT”))
    {
        m_CurrentPath = Directory.GetCurrentDirectory();
    }
    return m_CurrentPath;
}
}
public Configs()
{
	}
}
	
C)window/linux中目录及斜线的处理.
示例:
	
//方案1 运行时自带 var path1 = Path.Combine("xxx", "yyy", "zzz");
//方案2 反斜杠 两个平台都可以用 var path2 = ("xxx/yyy/zzz");
//方案3 根据不同环境生成不同文件路径,GetRuntimeDirectory 自己编写 //判断平台环境,路径可以任意格式"xxx/yyy\\zzz" //实际上多个开发协同的时候就是比较混乱,开发环境都没问题,集成的时候报错频繁 var path3 = GetRuntimeDirectory("xxx/yyy/zzz");
	
源码:
	public static string GetRuntimeDirectory(string path)
{
    //ForLinux
    if (IsLinuxRunTime())
        return GetLinuxDirectory(path);
    //ForWindows
    if (IsWindowRunTime())
        return GetWindowDirectory(path);
    return path;
}
//OSPlatform.Windows监测运行环境
public static bool IsWindowRunTime()
{ 
    return System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
}
//OSPlatform.Linux运行环境
public static bool IsLinuxRunTime()
{
    return System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
}
public static string GetLinuxDirectory(string path)
{
    string pathTemp = Path.Combine(path);
    return pathTemp.Replace("\\", "/");
}
public static string GetWindowDirectory(string path)
{
    string pathTemp = Path.Combine(path);
    return pathTemp.Replace("/", "\\");
}
	
D)asp.net core 中获取当前程序的目录.
	
    public class HomeController : Controller
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        public HomeController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }
        public ActionResult Index()
        {
            string webRootPath = _hostingEnvironment.WebRootPath;
            string contentRootPath = _hostingEnvironment.ContentRootPath;
            return Content(webRootPath + "\n" + contentRootPath);
        }
    }
E)获取一些系统常用的目录.
	Environment.SpecialFolder命名空间可以获取很多特定的路径目录。
    Environment.SpecialFolder. 
      ApplicationData 
      CommonApplicationData 
      CommonProgramFiles 
      Cookies 
      DesktopDirectory 
      Favorites 
      History 
      InternetCache 
      LocalApplicationData 
      Personal 
      ProgramFiles 
      Programs 
      Recent 
      SendTo 
      StartMenu 
      Startup 
      Templates 
      System  
F)一些使用中遇到的特殊问题.
F1).net core 程序打包后不同获取目录的方式可能和未打包之前的结果不一样.
注: dotnet publish -r win10-x64 /p:PublishSingleFile=true /p:PublishTrimmed=true
System.Environment.CurrentDirectory //.net core publish -PublishSingleFile=true 打包后依然正常
System.AppDomain.CurrentDomain.BaseDirectory //打包后变为一个c盘的一个用户目录下去了.常见问题
- 
                                                .net core 使用System.AppDomain.CurrentDomain.BaseDirectory获取目录并通过Publish PublishSingleFile命令打包后打包路径获取的值为c盘的appdata目录了
                                                
【解决方案】建议改为:System.Environment.CurrentDirectory 
快速入门
无