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.
HttpClientStudy/Docs/2.1.内核中的各种路径.ipynb

126 lines
4.6 KiB
Plaintext

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