You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
3.4 KiB
C#

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
namespace HttpClientStudy.Core.Utilities
{
/// <summary>
/// 应用工具类
/// </summary>
public class AppUtility
{
/// <summary>
/// 获取应用运行时各种路径
/// </summary>
/// <returns></returns>
public static IDictionary<string,(string desc,string? path)> GetApplicationPaths()
{
var pathDic = new Dictionary<string, (string desc, string? path)>()
{
//当前运行的exe的完整路径包含exe文件名只用于WinForm
{"Application.ExecutablePath",("程序集基完整路径(仅WinForm)", "Application.ExecutablePath 只适用于WinForm") },
//程序的启动路径只用于WinForm
{"Application.StartupPath",("程序集启动路径(仅WinForm)", "Application.StartupPath 只适用于WinForm") },
//当前执行的exe或启动项目的路径通过AppContext
{"AppContext.BaseDirectory",("执行或启动路径",AppContext.BaseDirectory) },
//当前执行的exe的目录不包含exe名使用AppDomain
{"AppDomain.CurrentDomain.BaseDirectory",("程序集解析程序用于探测程序集的基目录",AppDomain.CurrentDomain.BaseDirectory) },
//程序安装或启动基目录 包含应用程序的目录的名称
{"AppDomain.CurrentDomain.SetupInformation.ApplicationBase",("程序安装或启动基目录",AppDomain.CurrentDomain.SetupInformation.ApplicationBase) },
//当前进程的主模块路径包含exe名
{"Process.GetCurrentProcess().MainModule.FileName",("当前进程的主模块路径",Process.GetCurrentProcess()?.MainModule?.FileName) },
//环境变量:用户当前工作目录的完整限定路径
{"Environment.CurrentDirectory",("用户当前工作目录的完整限定路径",Environment.CurrentDirectory) },
//环境变量当前exe的完整路径包含exe名通过命令行参数
{"Environment.GetCommandLineArgs()[0]",("当前exe的完整路径",Environment.GetCommandLineArgs()[0]) },
//当前工作目录的路径(可变)
{"Directory.GetCurrentDirectory",("当前工作目录的路径(可变)",Directory.GetCurrentDirectory()) },
//当前Assembly的加载路径包含dll或exe名
{"Assembly.GetExecutingAssembly().Location",("当前Assembly的加载路径",Assembly.GetExecutingAssembly().Location) },
//入口程序集的路径
{"Assembly.GetEntryAssembly().Location",("入口程序集的路径",Assembly.GetEntryAssembly()?.Location) },
//已过时当前程序集的CodeBase路径可能为file URI格式
{"Assembly.GetExecutingAssembly().CodeBase",("当前程序集的CodeBase路径",Assembly.GetExecutingAssembly()?.CodeBase) },
//已过时入口程序集的CodeBase路径可能为file URI格式
{"Assembly.GetEntryAssembly().CodeBase",("入口程序集的CodeBase路径",Assembly.GetEntryAssembly()?.CodeBase) },
};
return pathDic;
}
}
}