Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取应用程序文件夹路径的最佳方法_C#_.net - Fatal编程技术网

C# 获取应用程序文件夹路径的最佳方法

C# 获取应用程序文件夹路径的最佳方法,c#,.net,C#,.net,我发现有一些方法可以获取应用程序文件夹路径: Application.StartupPath System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutionGassembly().Location) AppDomain.CurrentDomain.BaseDirectory System.IO.Directory.GetCurrentDirectory() Environment.CurrentDirectory

我发现有一些方法可以获取应用程序文件夹路径:

  • Application.StartupPath
  • System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutionGassembly().Location)
  • AppDomain.CurrentDomain.BaseDirectory
  • System.IO.Directory.GetCurrentDirectory()
  • Environment.CurrentDirectory
  • System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutionGassembly().GetName().CodeBase)
  • System.IO.Path.GetDirectory(Application.ExecutablePath)

  • 根据具体情况,最好的方法是什么?

    请注意,并非所有这些方法都返回相同的值。在某些情况下,它们可以返回相同的值,但请注意,它们的用途不同:

    Application.StartupPath
    
    返回
    StartupPath
    参数(可在运行应用程序时设置)


    返回当前目录,该目录可能是应用程序所在的文件夹,也可能不是。
    Environment.CurrentDirectory
    也是如此。如果您在DLL文件中使用它,它将返回进程运行的路径(在ASP.NET中尤其如此)。

    AppDomain.CurrentDomain.BaseDirectory
    对于访问位置相对于应用程序安装目录的文件可能最有用

    在ASP.NET应用程序中,这将是应用程序根目录,而不是bin子文件夹-这可能是您通常想要的。在客户端应用程序中,它将是包含主可执行文件的目录

    在VSTO 2005应用程序中,它将是包含应用程序的VSTO托管程序集的目录,而不是Excel可执行文件的路径

    其他人可能会根据您的环境返回不同的目录-例如,请参阅@Vimvq1987的答案

    CodeBase
    是找到文件的地方,可以是以http:///开头的URL。在这种情况下,
    位置
    可能是程序集下载缓存。不保证为中的程序集设置代码库

    更新
    现在(.NET Core、.NET Standard 1.3+或.NET Framework 4.6+)最好使用
    AppContext.BaseDirectory
    ,而不是
    AppDomain.CurrentDomain.BaseDirectory
    。两者都是等效的,但是。

    对于web应用程序,要获取当前web应用程序根目录,通常通过网页调用当前传入的请求:

    HttpContext.Current.Server.MapPath();
    
    System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
    

  • Application.StartupPath
    和7
    System.IO.Path.GetDirectoryName(Application.ExecutablePath)
    -仅适用于

  • System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutionGassembly().Location)

    将为您提供如下内容:
    “C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\legal services\\e84f415e\\96c98009\\assembly\\dl3\\42aaba80\\bcf9fd83\u 4b63d101”
    ,这就是您正在运行的页面所在的位置


  • AppDomain.CurrentDomain.BaseDirectory
    对于web应用程序可能很有用,并将返回类似于
    “C:\\hg\\Services\\Services\\Services.Website\\”
    的内容,这是基本目录,非常有用

  • System.IO.Directory.GetCurrentDirectory()
    和5<代码>环境.当前目录

  • 将为您获取进程启动的位置-因此对于从Visual Studio以调试模式运行的web应用程序,类似于
    “C:\\Program Files(x86)\\IIS Express”

  • System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutionGassembly().GetName().CodeBase)
  • 将获取运行代码的
    .dll
    的位置,对于可能是
    的web应用程序,“文件:\\C:\\hg\\Services\\Services\\Services.Website\\bin”

    例如,现在控制台应用程序点2-6将是
    .exe
    文件所在的目录


    希望这能为您节省一些时间。

    在会话中,我从实际登录的用户(在任务管理器会话1而不是0中)通过Win32 API从Windows服务启动了一个进程。在这里我们可以知道,哪个变量是最好的

    对于上述问题中的所有7个案例,结果如下:

    Path1: C:\Program Files (x86)\MyProgram
    Path2: C:\Program Files (x86)\MyProgram
    Path3: C:\Program Files (x86)\MyProgram\
    Path4: C:\Windows\system32
    Path5: C:\Windows\system32
    Path6: file:\C:\Program Files (x86)\MyProgram
    Path7: C:\Program Files (x86)\MyProgram
    

    当你们为自己的案例搜索最佳变量时,做同样的事情可能对你们中的一些人有帮助。

    我已经成功地使用了这个变量

    System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)
    

    它甚至可以在这个
    System.IO.Path.GetDirectory(Application.ExecutablePath)
    中工作。根据我的经验,最好的方法是将它们结合起来

  • System.Reflection.Assembly.getExecutionGassembly().GetName().CodeBase
    我会把垃圾箱文件夹给你
  • Directory.GetCurrentDirectory()
    在.NETCore上可以正常工作,但在.Net上不行,它将为您提供项目的根目录
  • System.AppContext.BaseDirectory
    AppDomain.CurrentDomain.BaseDirectory
    在.Net中运行良好,但在.NETCore中不起作用,它将为您提供项目的根目录

  • 在一个以.Net和.Net core为目标的类库中,我检查哪个框架承载该库,并选择一个或另一个。

    如果您知道获取根目录:

    string rootPath = Path.GetPathRoot(Application.StartupPath)
    
    DriveInfo cDrive = new DriveInfo(System.Environment.CurrentDirectory);
    var driverPath = cDrive.RootDirectory;
    
    根目录:

    string rootPath = Path.GetPathRoot(Application.StartupPath)
    
    DriveInfo cDrive = new DriveInfo(System.Environment.CurrentDirectory);
    var driverPath = cDrive.RootDirectory;
    

    为什么我们有很多方法来获取应用程序的路径。我认为每种方法都有其原因。在#6中有一个错误:应该是:System.Reflection.Assembly.GetExecutionGassembly().GetName().CodeBase),System.IO.Path.GetDirectoryName(Application.ExecutablePath)hooray for#6,当我在一个web项目中时,我不想在IoC加载的库中使用Server.MapPath逻辑,该库在性质上不是特定于web的。我们现在有了可靠的
    IHostEnvironment.ContentRoo