简体   繁体   English

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

[英]What is the best way to determine application root directory?

I need to get all dlls in my application root directory. 我需要在我的应用程序根目录中获取所有dll。 What is the best way to do that? 最好的方法是什么?

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? 还有更好的方法吗?

AppDomain.CurrentDomain.BaseDirectory is my go to way of doing so. AppDomain.CurrentDomain.BaseDirectory是我这样做的方式。

However: 然而:

Application.StartupPath gets the directory of your executable Application.StartupPath获取可执行文件的目录

AppDomain.BaseDirectory gets 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,除非您关心程序集解析。

It depends. 这取决于。 If you want the directory of the EXE that started the application, then either of your two examples will work. 如果您想要启动应用程序的EXE目录,那么您的两个示例中的任何一个都可以使用。 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. 但请记住,.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. 然后我知道我将所有DLL都放在与特定程序集相同的目录中。 For example, if you have an application MyApp.exe with a class in it MyApp.MyClass, then you would do this; 例如,如果您的应用程序MyApp.exe中有一个类MyApp.MyClass,那么您可以这样做;

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

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 49
  • 306 306
  • 166 166
  • 26 26
  • 201 201

So it seems AppDomain.CurrentDomain.BaseDirectory is the way to go. 所以似乎AppDomain.CurrentDomain.BaseDirectory是要走的路。

如果要获取应用程序根文件夹路径,请使用下面的代码示例。

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

I use 我用

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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 清空目录的最佳方法是什么? - What is the best way to empty a directory? 确定NamedDataSlot是否存在的最佳方法是什么 - What's the best way to determine if NamedDataSlot exists 确定的最佳方法是双倍不是零 - what is the best way to determine is a double is not zero WinForms应用程序确定运行哪个Windows操作系统的最佳方法是什么? - What is the best way for a WinForms application to determine exactly which Windows operating system it is running on? 确定应用程序位置的正确方法是什么? - What is the proper way to determine an application's location? 在 ASP.NET 核心应用程序中访问活动目录的最佳方式是什么? - What is the best way acces active directory in an ASP.NET Core application? 通过C#运行应用程序的无处不在的方式来获取根目录 - Ubiquitous way to get the root directory an application is running in via C# 在C#中,确定小数是否为“ int”的最佳方法是什么? - In C#, what is the best way to determine if a decimal “is an int”? 确定收集对象的初始容量的最佳方法是什么? - What is the best way to determine the initial capacity for collection objects? 确定表是否已包含具有特定ID的记录的最佳方法是什么? - What is the best way to determine if table already has record with specific ID?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM