当前位置:首页 > 码农资料 > 正文内容

C#获取当前路径

CCSSRW3年前 (2022-02-28)码农资料2355

使用 AppDomain.CurrentDomain.BaseDirectory

这种方法适用于大多数情况,返回包含应用程序的目录

using System;
class Program
{
    static void Main()
    {
        string path = AppDomain.CurrentDomain.BaseDirectory;
        Console.WriteLine(path);
    }
}


获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称,适用于 Windows Forms。

private void PrintStartupPath()
{
   MessageBox.Show( "The path for the executable file that " +
      "started the application is: " +
      Application.StartupPath);
}


获取启动了应用程序的可执行文件的路径,包括可执行文件的名称,适用于 Windows Forms。

private void PrintStartupPath()
{
   MessageBox.Show( "The path for the executable file that " +
      "started the application is: " +
      Application.ExecutablePath);
}



扫描二维码推送至手机访问

本文链接:http://xinrui.ren/post/124.html