简体   繁体   English

我应该使用 AppDomain.CurrentDomain.BaseDirectory 还是 System.Environment.CurrentDirectory?

[英]Should I use AppDomain.CurrentDomain.BaseDirectory or System.Environment.CurrentDirectory?

I have two exe files in the same folder, I can run exe2 from a button in exe1.我在同一个文件夹中有两个 exe 文件,我可以从 exe1 中的一个按钮运行 exe2。 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.今天我通过远程(终端服务)session 观察客户,exe2 未能运行“找不到文件”错误,但我们检查时 exe1 在同一目录中。 So should I be using AppDomain.CurrentDomain.BaseDirectory or System.Environment.CurrentDirectory ?那么我应该使用AppDomain.CurrentDomain.BaseDirectory还是System.Environment.CurrentDirectory

Thanks谢谢

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. Environment.CurrentDirectory是一个可以并且将在运行应用程序的过程中发生变化的值。 For instance, using default parameters, the OpenFileDialog in WinForms will change this value to the directory where the file was selected from.例如,使用默认参数,WinForms 中的 OpenFileDialog 会将此值更改为从中选择文件的目录。

AppDomain.CurrentDomain.BaseDirectory returns the directory from where the current application domain was loaded. AppDomain.CurrentDomain.BaseDirectory返回加载当前应用程序域的目录。
System.Environment.CurrentDirectory returns the current system directory. System.Environment.CurrentDirectory返回当前系统目录。
In your case AppDomain.CurrentDomain.BaseDirectory is the best solution.在您的情况下, AppDomain.CurrentDomain.BaseDirectory是最好的解决方案。

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;   

As I understand it, you should use BaseDirectory .据我了解,您应该使用BaseDirectory CurrentDirectory could change over the course of the program's execution. CurrentDirectory可能会在程序执行过程中发生变化。

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).在 Visual Studio 2010 测试项目中,如果启用编辑测试设置的部署选项,AppDomain.CurrentDomain.BaseDirectory 指向 TestResults\Out 文件夹(不是 bin\debug)。 Although, default setting point to bin\debug folder.虽然,默认设置指向 bin\debug 文件夹。

Here I found convincing answer.在这里,我找到了令人信服的答案。

Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app? 为什么 AppDomain.CurrentDomain.BaseDirectory 在 asp.net 应用程序中不包含“bin”?

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所以,正如所有人所说,我们应该始终使用 go

System.AppDomain.CurrentDomain.BaseDirectory;

as it checks the Current Domain directory for the path.因为它检查当前域目录的路径。

have a look for more information查看更多信息

Could not find a part of path error on server 在服务器上找不到部分路径错误

I usually use something like:我通常使用类似的东西:

            string AppPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            AppPath = AppPath.Replace("file:\\", "");

暂无
暂无

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

相关问题 AppDomain.CurrentDomain.BaseDirectory 更改为错误的目录 - AppDomain.CurrentDomain.BaseDirectory changes to wrong directory 如何模拟AppDomain.CurrentDomain.BaseDirectory - How to mock AppDomain.CurrentDomain.BaseDirectory AppDomain.CurrentDomain.BaseDirectory会根据应用程序的目标平台进行更改 - AppDomain.CurrentDomain.BaseDirectory changes according to the app's target platform C#Uri AppDomain.CurrentDomain.BaseDirectory相对路径 - C# Uri AppDomain.CurrentDomain.BaseDirectory relative path 为什么 AppDomain.CurrentDomain.BaseDirectory 在控制台应用程序中定位 bin catelog? - Why is AppDomain.CurrentDomain.BaseDirectory targeting bin catelog in console application? 运行MVC项目时AppDomain.CurrentDomain.BaseDirectory路径问题 - AppDomain.CurrentDomain.BaseDirectory path issue when running MVC project AppDomain.CurrentDomain.BaseDirectory运行测试后会发生变化吗? - AppDomain.CurrentDomain.BaseDirectory changes after running test? System.Environment.CurrentDirectory 返回错误的目录 - System.Environment.CurrentDirectory returns wrong Directory 编写 windows 窗体应用程序时 Application.StartupPath 和 AppDomain.CurrentDomain.BaseDirectory 之间的首选项 - Preference between Application.StartupPath and AppDomain.CurrentDomain.BaseDirectory when writing a windows forms application 如何通过在 c# 中使用 AppDomain.CurrentDomain.BaseDirectory 在文件夹路径中上一步 - How to go one step above in a folder path by using AppDomain.CurrentDomain.BaseDirectory in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM