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

   2024-09-30 6310
核心提示:在C#中,你可以使用System.IO.Path和System.Reflection命名空间来获取当前程序的路径。下面是一个示例代码:using System;using

在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);

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号