温馨提示×

c#如何获取当前程序的路径

c#
小亿
161
2024-09-05 04:54:57
栏目: 编程语言

在C#中,你可以使用System.IO.PathSystem.Reflection命名空间来获取当前程序的路径。下面是一个示例代码:

using System; using System.IO; using System.Reflection; class Program { static void Main() { // 获取当前执行程序的路径 string currentPath = Assembly.GetExecutingAssembly().Location; // 输出结果 Console.WriteLine("当前程序的路径: " + currentPath); } } 

这段代码将会输出当前正在运行的程序的完整路径。如果你只需要程序所在的目录,可以使用Path.GetDirectoryName()方法:

string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); Console.WriteLine("当前程序所在的目录: " + currentDir); 

0