是什么让当前目录中的这些方法之间的区别? [英] What is the difference between these ways of getting current directory?

查看:144
本文介绍了是什么让当前目录中的这些方法之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们都产生相同的结果,文件夹包含与正在执行的EXE的位置。我相信有在.NET基础类库没有好坏的方法。他们是在特殊情况下皆宜。哪一个是适合哪个场景

  VAR appBaseDir = AppDomain.CurrentDomain.BaseDirectory?; 
VAR currentDir = Environment.CurrentDirectory;
VAR DIR = Directory.GetCurrentDirectory();
VAR路径= Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly()位置。);


解决方案

他们都给予了同样的结果。




他们当然不知道。 currentDir DIR 这两个给你的当前工作目录的 - 默认的目录,即你的可执行文件运行的的(但是可以在执行过程中改变)。



相反, appBaseDir 路径获得包含执行程序集的文件的目录。



要说明他们是如何不同,假设您拥有它坐落在的可执行文件C:\bar\baz.exe

  $ MD C:\foo $ B现在我可以通过在终端中输入下面的命令链执行应用程序$ b $ CD C:\foo 
$ ..\bar\baz.exe

现在当前的工作目录是 C:\foo 但应用程序的基本目录为 C:\bar 。存在设置启动应用程序的其他方法的工作目录类似手段(通过例如,通过一个快捷方式图标或编程,如的Process.Start )。



不过,该框架提供了访问这些信息的不同方式:



Environment.CurrentDirectory 很直接地传达执行环境(环境变量),被查询的含义。 Directory.GetCurrentDirectory()可以的实际做内部相同的(我不知道),但它封装了这一点,而专注于为用户提供用于查询目录信息的逻辑API。



AppDomain.CurrentDomain 有关于当前的AppDomain (大致的可执行文件)。这些信息一部分是在逻辑上,的AppDomain 的路径。相比之下, System.Reflection.Assembly 为您提供有关的组装的一般信息的 - 这些代表任何在.NET中的二进制对象,包括DLL和EXE文件的。 GetExecutingAssembly 特别返回当前执行的程序集。你可以得到的及其的再通过查询其位置属性,它给出了一个汇编文件的物理路径的路径。


They all give the same result, the location of folder that contains the exe that is being executed. I am sure there are no good or bad methods in the .net BCL. They are all appropriate in particular circumstances. Which one is appropriate for which scenario?

var appBaseDir = AppDomain.CurrentDomain.BaseDirectory; 
var currentDir = Environment.CurrentDirectory; 
var dir = Directory.GetCurrentDirectory(); 
var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

解决方案

They all give the same result

They certainly don’t. currentDir and dir both give you the current working directory – i.e. by default the directory your executable was run from (but it can be changed during the execution).

By contrast, appBaseDir and path get the directory which contains the executing assembly’s file.

To illustrate how they differ, consider that you have an executable which sits in C:\bar\baz.exe. Now I can execute the application by entering the following chain of commands in a terminal:

$ md C:\foo
$ cd C:\foo
$ ..\bar\baz.exe

Now the current working directory is C:\foo but the application’s base directory is C:\bar. There exist analogous means of setting the working directory for other methods of launching an application (e.g. via a shortcut icon or programmatically, such as via Process.Start).

Still, the framework provides different ways of accessing this information:

Environment.CurrentDirectory quite directly conveys the meaning that the execution environment (an environment variable) is queried. Directory.GetCurrentDirectory() may actually do the same internally (I have no idea) but it encapsulates this, and rather focuses on providing the user with a logical API for querying information about directories.

AppDomain.CurrentDomain has information about the current AppDomain (roughly, the executable). Part of that information is, logically, the AppDomain’s path. By contrast, System.Reflection.Assembly gives you general information about assembles – these represent any kind of binary objects in .NET, including DLLs and EXEs. GetExecutingAssembly in particular returns the currently executed assembly. And you can get its path again by querying its Location property, which gives the physical path of an assembly file.

这篇关于是什么让当前目录中的这些方法之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆