{ "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, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" }, "vscode": { "languageId": "polyglot-notebook" } }, "outputs": [], "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() \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", "\n", " //环境变量:用户当前工作目录的完整限定路径\n", " {\"Environment.CurrentDirectory\",(\"用户当前工作目录的完整限定路径\",Environment.CurrentDirectory) }, \n", "\n", " //环境变量:当前exe的完整路径,包含exe名,通过命令行参数\n", " {\"Environment.GetCommandLineArgs()[0]\",(\"当前exe的完整路径\",Environment.GetCommandLineArgs()[0]) }, \n", "\n", " //当前工作目录的路径(可变)\n", " {\"Directory.GetCurrentDirectory\",(\"当前工作目录的路径(可变)\",Directory.GetCurrentDirectory()) },\n", " \n", " //当前Assembly的加载路径,包含dll或exe名\n", " {\"Assembly.GetExecutingAssembly().Location\",(\"当前Assembly的加载路径\",Assembly.GetExecutingAssembly().Location) },\n", "\n", " //入口程序集的路径\n", " {\"Assembly.GetEntryAssembly().Location\",(\"入口程序集的路径\",Assembly.GetEntryAssembly()?.Location) },\n", "\n", " //已过时:当前程序集的CodeBase路径,可能为file URI格式\n", " {\"Assembly.GetExecutingAssembly().CodeBase\",(\"当前程序集的CodeBase路径\",Assembly.GetExecutingAssembly()?.CodeBase) },\n", "\n", " //已过时:入口程序集的CodeBase路径,可能为file URI格式\n", " {\"Assembly.GetEntryAssembly().CodeBase\",(\"入口程序集的CodeBase路径\",Assembly.GetEntryAssembly()?.CodeBase) },\n", " };\n", "\n", " var message = string.Empty;\n", " foreach (var item in pathDic)\n", " {\n", " message += $\"{item.Key} => {item.Value.path}{Environment.NewLine}\";\n", " }\n", "\n", " Console.WriteLine(message);" ] } ], "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 }