C# 我应该使用 AppDomain.CurrentDomain.BaseDirectory 还是 System.Environment.CurrentDirectory?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/674857/
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
Should I use AppDomain.CurrentDomain.BaseDirectory or System.Environment.CurrentDirectory?
提问by
I have two exe files in the same folder, I can run exe2 from a button in exe1. Today I was observing a customer over a remote (terminal services) session and exe2 failed to run 'File not found' error, yet exe1 was in the same directory when we checked. So should I be using AppDomain.CurrentDomain.BaseDirectoryor System.Environment.CurrentDirectory?
我在同一个文件夹中有两个 exe 文件,我可以从 exe1 中的一个按钮运行 exe2。今天,我通过远程(终端服务)会话观察客户,exe2 无法运行“找不到文件”错误,但我们检查时 exe1 位于同一目录中。那么我应该使用AppDomain.CurrentDomain.BaseDirectory还是System.Environment.CurrentDirectory?
Thanks
谢谢
回答by Joel Coehoorn
As I understand it, you should use BaseDirectory
. CurrentDirectory
could change over the course of the program's execution.
据我了解,您应该使用BaseDirectory
. CurrentDirectory
可以在程序执行过程中改变。
回答by JaredPar
If you want to find files in the same directory as your application, AppDomain.CurrentDomain.BaseDirectory
is the correct choice.
如果要查找与您的应用程序在同一目录中的文件,AppDomain.CurrentDomain.BaseDirectory
则是正确的选择。
Environment.CurrentDirectory
is a value that can and will change throught the course of running your application. For instance, using default parameters, the OpenFileDialog in WinForms will change this value to the directory where the file was selected from.
Environment.CurrentDirectory
是一个可以并且将会在运行应用程序的过程中改变的值。例如,使用默认参数,WinForms 中的 OpenFileDialog 会将此值更改为从中选择文件的目录。
回答by Zahymaka
I usually use something like:
我通常使用类似的东西:
string AppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
AppPath = AppPath.Replace("file:\", "");
回答by Albert
AppDomain.CurrentDomain.BaseDirectory
returns the directory from where the current application domain was loaded.System.Environment.CurrentDirectory
returns the current system directory.
In your case AppDomain.CurrentDomain.BaseDirectory
is the best solution.
AppDomain.CurrentDomain.BaseDirectory
返回加载当前应用程序域的目录。System.Environment.CurrentDirectory
返回当前系统目录。
在你的情况下AppDomain.CurrentDomain.BaseDirectory
是最好的解决方案。
回答by OrganicCoder
In Visual studio 2010 test projects, if you enable deployment option of Edit test settings, AppDomain.CurrentDomain.BaseDirectory points to the TestResults\Out folder(not bin\debug). Although, default setting point to bin\debug folder.
在 Visual Studio 2010 测试项目中,如果启用 Edit test settings 的部署选项,AppDomain.CurrentDomain.BaseDirectory 指向 TestResults\Out 文件夹(而不是 bin\debug)。虽然,默认设置指向 bin\debug 文件夹。
Here I found convincing answer.
在这里,我找到了令人信服的答案。
Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app?
为什么 AppDomain.CurrentDomain.BaseDirectory 在 asp.net 应用程序中不包含“bin”?
回答by Dalorzo
You should use AppDomain.CurrentDomain.BaseDirectory
.
你应该使用AppDomain.CurrentDomain.BaseDirectory
.
For example in a windows services application:
例如在 Windows 服务应用程序中:
System.Environment.CurrentDirectory
will return C:\Windows\system32
System.Environment.CurrentDirectory
将返回C:\Windows\system32
While
尽管
AppDomain.CurrentDomain.BaseDirectory
will return [Application.exe location]
AppDomain.CurrentDomain.BaseDirectory
将返回 [Application.exe 位置]
Another important factor to note is that AppDomain.CurrentDomain.BaseDirectory
is a readonly property while the Environment.CurrentDirectory
can be something else if necessary:
另一个需要注意的重要因素是它AppDomain.CurrentDomain.BaseDirectory
是一个只读属性,而Environment.CurrentDirectory
如果需要,它可以是其他东西:
// Change the directory to AppDomain.CurrentDomain.BaseDirectory
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
回答by hud
I have also been through this few days back, as I was using
这几天我也经历过,因为我正在使用
Environment.CurrentDirectory
as it was giving me issue on the production server but was working fine with my local server,
因为它在生产服务器上给我带来了问题,但在我的本地服务器上运行良好,
So, I tried with
所以,我试过
System.AppDomain.CurrentDomain.BaseDirectory;
And it worked for me in both the Environment.
它在环境中对我都有效。
So, As all of them has said We should always go with
所以,正如他们所有人所说的,我们应该永远和
System.AppDomain.CurrentDomain.BaseDirectory;
as it checks the Current Domain directory for the path.
因为它会检查当前域目录的路径。
have a look for more information
看看更多信息