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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12874828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:46:52  来源:igfitidea点击:

Get application directory using C# Console Application?

c#consoledirectory

提问by a1204773

I found something on google but it not working on C# Console Application

我在谷歌上找到了一些东西,但它在 C# 控制台应用程序上不起作用

What I found:

我发现了什么:

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

How I can get application directory using c# Console Application?

如何使用 c# Console Application 获取应用程序目录?

采纳答案by Alex

If you still want to use Application.ExecutablePath in console application you need to:

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

  1. Add a reference to System.Windows.Forms namespace
  2. Add System.Windows.Forms to your usings section

    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);
            }
        }
    }
    
  1. 添加对 System.Windows.Forms 命名空间的引用
  2. 将 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);
            }
        }
    }
    

Also you can use Directory.GetCurrentDirectory()instead of Path.GetDirectoryName(Application.ExecutablePath)and thus you won't need a reference to System.Windows.Forms.

您也可以使用Directory.GetCurrentDirectory()代替,Path.GetDirectoryName(Application.ExecutablePath)因此您不需要对 System.Windows.Forms 的引用。

If you'd like not to include neither System.IOnor System.Windows.Formsnamespaces then you should follow Reimeus's answer.

如果您不想既不包含名称空间System.IO也不包含System.Windows.Forms名称空间,那么您应该遵循 Reimeus 的回答。

回答by Reimeus

Application is not available for Console Applications, it's for windows forms.

应用程序不适用于控制台应用程序,它适用于 Windows 窗体。

To get the working directory you can use

要获取您可以使用的工作目录

Environment.CurrentDirectory

Also to get the directory of the executable, you could use:

另外要获取可执行文件的目录,您可以使用:

AppDomain.CurrentDomain.BaseDirectory

回答by WhoIsRich

BEWARE, there are several methods and PITFALLS for paths.

请注意,路径有多种方法和陷阱。

  • What location are you after? The working directory, the .EXE directory, the DLLs directory?

  • Do you want code that also works in a service or console application?

  • Will your code break if the directory has inconsistent trailing slashes?

  • 你要找什么位置?工作目录、.EXE 目录、DLL 目录?

  • 您希望代码也适用于服务或控制台应用程序吗?

  • 如果目录的尾部斜杠不一致,您的代码会中断吗?

Lets look at some options:

让我们看看一些选项:

Application.ExecutablePath

Requires adding a reference and loading the Application namespace.

需要添加引用并加载应用程序命名空间。

Directory.GetCurrentDirectory
Environment.CurrentDirectory

If the program is run by shortcut, registry, task manager, these will give the 'Start In' folder, which can be different from the .EXE location.

如果程序通过快捷方式、注册表、任务管理器运行,这些将提供“开始于”文件夹,该文件夹可能与 .EXE 位置不同。

AppDomain.CurrentDomain.BaseDirectory

Depending on how it's run effects whether it includes a trailing slash. This can break things, for example GetDirectoryName() considers no slash to be a file, and will remove the last part.

取决于它的运行方式,它是否包含尾部斜杠。这可能会破坏一些东西,例如 GetDirectoryName() 将没有斜杠视为文件,并将删除最后一部分。

Either of these are my recommendation, working in both Form and Console applications:

这些都是我的建议,适用于 Form 和 Console 应用程序:

var AssemblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
or
var AssemblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

When used in a main program, both are indentical. If used within a DLL, the first returns the .EXE directory that loaded the DLL, the second returns the DLLs directory.

在主程序中使用时,两者是相同的。如果在 DLL 中使用,第一个返回加载 DLL 的 .EXE 目录,第二个返回 DLL 目录。

回答by Buddika Weerasinghe

I know it's been a long long time, but Assembly.GetExecutingAssembly().CodeBaseworked for me. You just have to replace Application.ExecutablePathwith it.

我知道这已经很长时间了,但Assembly.GetExecutingAssembly().CodeBase对我有用。你只需要替换Application.ExecutablePath它。

Of course you have to add using System.Reflection;

当然你必须添加 using System.Reflection;

I have tried using System.Windows.Formsbut it gave me an exception

我试过使用,System.Windows.Forms但它给了我一个例外