在C#程序中,可以使用 Assembly
类的 Location
属性来获取当前程序集的文件路径。以下是一个示例代码:
using System;
using System.Reflection;
class Program
{
static void Main()
{
// 获取当前程序集的文件路径
string assemblyPath = Assembly.GetExecutingAssembly().Location;
Console.WriteLine("程序集文件路径: " + assemblyPath);
// 获取当前程序的目录路径
string directoryPath = AppDomain.CurrentDomain.BaseDirectory;
Console.WriteLine("程序目录路径: " + directoryPath);
}
}
在上述示例中,Assembly.GetExecutingAssembly().Location
返回当前程序集的文件路径,而 AppDomain.CurrentDomain.BaseDirectory
返回当前程序的目录路径。可以根据具体需求选择使用其中的一个。