微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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的答案.

原文地址:https://www.jb51.cc/csharp/93302.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐