使用C#控制台应用程序获取应用程序目录?

我在google上找到了一些东西,但它并不适用于C#控制台应用程序

我发现:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

我如何使用c#控制台应用程序获取应用程序目录?

解决方法

如果您仍然希望在控制台应用程序中使用Application.ExecutablePath,您需要:

>添加对System.Windows.Forms命名空间的引用
>将System.Windows.Forms添加到您的使用部分

using System;
using System.IO;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string appDirectory = Path.GetDirectoryName(Application.ExecutablePath);
            Console.WriteLine(appDirectory);
        }
    }
}

此外,您可以使用Directory.GetCurrentDirectory()而不是Path.GetDirectoryName(Application.ExecutablePath),因此您不需要引用System.Windows.Forms.

如果你不想不包括System.IO和System.Windows.Forms命名空间,那么你应该遵循Reimeus的答案.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...