确定应用程序根目录的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/362790/
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-04 00:35:18  来源:igfitidea点击:

What is the best way to determine application root directory?

c#.netwinforms

提问by abatishchev

I need to get all dlls in my application root directory. What is the best way to do that?

我需要在我的应用程序根目录中获取所有 dll。最好的方法是什么?

string root = Application.StartupPath;

Or,

或者,

string root = new FileInfo(Assembly.GetExecutingAssembly().Location).FullName;

And after that,

在那之后,

Directory.GetFiles(root, "*.dll");

Which way is better? Are there better ways?

哪种方式更好?有更好的方法吗?

采纳答案by Greg Dean

AppDomain.CurrentDomain.BaseDirectoryis my go to way of doing so.

AppDomain.CurrentDomain.BaseDirectory是我这样做的方式。

However:

然而:

Application.StartupPathgets the directory of your executable

Application.StartupPath获取可执行文件的目录

AppDomain.BaseDirectorygets the directory used to resolve assemblies

AppDomain.BaseDirectory获取用于解析程序集的目录

Since they can be different, perhaps you want to use Application.StartupPath, unless you care about assembly resolution.

由于它们可能不同,也许您想使用 Application.StartupPath,除非您关心程序集解析。

回答by Rob Prouse

It depends. If you want the directory of the EXE that started the application, then either of your two examples will work. Remember though, that .NET is very flexible, and it could be that another application has linked to your EXE and is calling it, possibly from another directory.

这取决于。如果您想要启动应用程序的 EXE 目录,那么您的两个示例中的任何一个都可以使用。但请记住,.NET 非常灵活,可能是另一个应用程序链接到您的 EXE 并正在调用它,可能是从另一个目录。

That doesn't happen very often and you would probably have written if it did, but it is a possibility. Because of that, I prefer to specify which assembly I am interested in and get the directory from that. Then I know that I am getting all of the DLLs in the same directory as that specific assembly. For example, if you have an application MyApp.exe with a class in it MyApp.MyClass, then you would do this;

这种情况并不经常发生,如果发生了,您可能会写,但这是一种可能性。因此,我更喜欢指定我感兴趣的程序集并从中获取目录。然后我知道我将所有 DLL 都放在与该特定程序集相同的目录中。例如,如果您有一个 MyApp.exe 应用程序,其中有一个类 MyApp.MyClass,那么您应该这样做;

string root = string.Empty;
Assembly ass = Assembly.GetAssembly( typeof( MyApp.MyClass ) );
if ( ass != null )
{
   root = ass.Location;
}

回答by Telan Niranga

If you want to get the application root folder path use below code sample.

如果您想获取应用程序根文件夹路径,请使用以下代码示例。

string path =new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName

回答by eried

This is an old question but I always used to use:

这是一个老问题,但我总是习惯使用:

Environment.CurrentDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

However, looking at the solutions in here I think some simple tests need to be done:

然而,看看这里的解决方案,我认为需要做一些简单的测试:

var r = new List<long>();
var s = Stopwatch.StartNew();

s.Restart();
string root1 = Application.StartupPath;
r.Add(s.ElapsedTicks);

s.Restart();
string root2 = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
r.Add(s.ElapsedTicks);

s.Restart();
string root3 = Path.GetDirectoryName(new FileInfo(Assembly.GetExecutingAssembly().Location).FullName);
r.Add(s.ElapsedTicks);

s.Restart();
string root4 = AppDomain.CurrentDomain.BaseDirectory;
r.Add(s.ElapsedTicks);

s.Restart();
string root5 = Path.GetDirectoryName(Assembly.GetAssembly( typeof( Form1 ) ).Location);
r.Add(s.ElapsedTicks);

The results in ticks:

结果以刻度为单位:

  • 49
  • 306
  • 166
  • 26
  • 201
  • 49
  • 306
  • 166
  • 26
  • 201

So it seems AppDomain.CurrentDomain.BaseDirectoryis the way to go.

所以这似乎AppDomain.CurrentDomain.BaseDirectory是要走的路。

回答by Someguy

I use

我用

Path.GetDirectoryName(new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath)

Path.GetDirectoryName(new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath)

The Assembly.Location will point to the shadow copy if you use shadow copying, so using CodeBase is a better option, but CodeBase is a Url.

如果您使用卷影复制,Assembly.Location 将指向卷影副本,因此使用 CodeBase 是更好的选择,但 CodeBase 是一个 Url。