{
"cells": [
"cell_type": "markdown",
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
"source": [
"# 多语言笔记:内核中的各种路径"
]
"metadata": {},
"搞清楚,内核执行时的各种默认目录、路径,方便解决一些引用、文件等跟路径有关的问题。"
"cell_type": "code",
"execution_count": null,
"vscode": {
"languageId": "polyglot-notebook"
"outputs": [],
"// C#中,内核执行时,各种默认路径如下:\n",
"\n",
"using System;\n",
"using System.IO;\n",
"using System.Threading;\n",
"using System.Threading.Tasks;\n",
"using System.Diagnostics;\n",
"using System.Reflection;\n",
"using Microsoft.Extensions.DependencyInjection;\n",
" var pathDic = new Dictionary<string, (string desc, string? path)>() \n",
" {\n",
" //当前运行的exe的完整路径,包含exe文件名,只用于WinForm\n",
" {\"Application.ExecutablePath\",(\"程序集基完整路径(仅WinForm)\", \"Application.ExecutablePath 只适用于WinForm\") },\n",
" //程序的启动路径:只用于WinForm\n",
" {\"Application.StartupPath\",(\"程序集启动路径(仅WinForm)\", \"Application.StartupPath 只适用于WinForm\") },\n",
" //当前执行的exe或启动项目的路径,通过AppContext\n",
" {\"AppContext.BaseDirectory\",(\"执行或启动路径\",AppContext.BaseDirectory) }, \n",
" //当前执行的exe的目录,不包含exe名,使用AppDomain\n",
" {\"AppDomain.CurrentDomain.BaseDirectory\",(\"程序集解析程序用于探测程序集的基目录\",AppDomain.CurrentDomain.BaseDirectory) }, \n",
" //程序安装或启动基目录 包含应用程序的目录的名称\n",
" {\"AppDomain.CurrentDomain.SetupInformation.ApplicationBase\",(\"程序安装或启动基目录\",AppDomain.CurrentDomain.SetupInformation.ApplicationBase) }, \n",
" //当前进程的主模块路径,包含exe名\n",
" {\"Process.GetCurrentProcess().MainModule.FileName\",(\"当前进程的主模块路径\",Process.GetCurrentProcess()?.MainModule?.FileName) }, \n",
" //环境变量:用户当前工作目录的完整限定路径\n",
" {\"Environment.CurrentDirectory\",(\"用户当前工作目录的完整限定路径\",Environment.CurrentDirectory) }, \n",
" //环境变量:当前exe的完整路径,包含exe名,通过命令行参数\n",
" {\"Environment.GetCommandLineArgs()[0]\",(\"当前exe的完整路径\",Environment.GetCommandLineArgs()[0]) }, \n",
" //当前工作目录的路径(可变)\n",
" {\"Directory.GetCurrentDirectory\",(\"当前工作目录的路径(可变)\",Directory.GetCurrentDirectory()) },\n",
" \n",
" //当前Assembly的加载路径,包含dll或exe名\n",
" {\"Assembly.GetExecutingAssembly().Location\",(\"当前Assembly的加载路径\",Assembly.GetExecutingAssembly().Location) },\n",
" //入口程序集的路径\n",
" {\"Assembly.GetEntryAssembly().Location\",(\"入口程序集的路径\",Assembly.GetEntryAssembly()?.Location) },\n",
" //已过时:当前程序集的CodeBase路径,可能为file URI格式\n",
" {\"Assembly.GetExecutingAssembly().CodeBase\",(\"当前程序集的CodeBase路径\",Assembly.GetExecutingAssembly()?.CodeBase) },\n",
" //已过时:入口程序集的CodeBase路径,可能为file URI格式\n",
" {\"Assembly.GetEntryAssembly().CodeBase\",(\"入口程序集的CodeBase路径\",Assembly.GetEntryAssembly()?.CodeBase) },\n",
" };\n",
" var message = string.Empty;\n",
" foreach (var item in pathDic)\n",
" message += $\"{item.Key} => {item.Value.path}{Environment.NewLine}\";\n",
" }\n",
" Console.WriteLine(message);"
],
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
"language_info": {
"name": "python"
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
"aliases": [],
"name": "csharp"
"nbformat": 4,
"nbformat_minor": 2