简体   繁体   English

Directory.GetCurrentDirectory()根据命令行参数返回不同的结果

[英]Directory.GetCurrentDirectory() returns different results based on command line arguments

I'm hoping someone can explain why Directory.GetCurrentDirectory() returns different results based on how I pass my command line arguments to the application (running with args vs dragging a folder over the app.exe) 我希望有人可以解释为什么Directory.GetCurrentDirectory()根据我将命令行参数传递给应用程序的方式返回不同的结果(使用args运行vs在app.exe上拖动文件夹)

To jump right into it consider this piece of code: 要直接进入它,请考虑以下代码:

 public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("The current directory is {0}", Directory.GetCurrentDirectory());

        if(args != null && args.Any())
            Console.WriteLine("Command line arguments are {0}", String.Join(", ", args));

        Console.ReadLine();
    }
}


If you build and run this using the command prompt as shown below the output is what you'd expect. 如果使用命令提示符构建并运行它,如下所示,则输出就是您所期望的。 It will output the current directory the application resides in. 它将输出应用程序所在的当前目录。

C:\Projects\ArgumentTest\ApplicationA\bin\Debug\ApplicationA.exe C:\mydirectory
The current directory is C:\Projects\ArgumentTest\ApplicationA\bin\Debug\
Command line arguments are C:\mydirectory


If you build and run this program by dragging files or folders over the application you get different results. 如果通过在应用程序上拖动文件或文件夹来构建和运行此程序,则会得到不同的结果。 Instead of returning the expected result instead Directory.GetCurrentDirectory() returns the path to the first file you've dragged over the application. 而不是返回预期的结果而不是Directory.GetCurrentDirectory()返回您在应用程序上拖动的第一个文件的路径。


I've currently got a work around for this issue however, I am keen to understand why this is happening. 我现在有一个解决这个问题的方法,但我很想知道为什么会这样。

Other info: 其他信息:

  • .NET 4.5 .NET 4.5
  • Windows 2012R2 (Virtual machine) Windows 2012R2(虚拟机)
  • Full administrator rights on the machine 机器的完全管理员权限

Hopefully someone can provide some insight. 希望有人可以提供一些见解。

I think the problem here is your expectation. 我认为这里的问题是你的期望。 In particular, this bit: 特别是这一点:

It will output the current directory the application resides in. 它将输出应用程序所在的当前目录。

That is not what I expect from GetCurrentDirectory() . 不是我对GetCurrentDirectory()期望。 The current directory is a feature of the calling context, not the application. 当前目录是调用上下文的一个功能,而不是应用程序。 If I run the executable via a full or relative path (rather than just foo.exe ), I expect GetCurrentDirectory() to return the directory that I am in - not the directory that the application is in. In the case of dragging files over it: frankly, GetCurrentDirectory() is largely undefined, but the directory of the first file is not unreasonable. 如果我通过完整或相对路径(而不仅仅是foo.exe )运行可执行文件,我希望GetCurrentDirectory()返回我所在的目录 - 而不是应用程序所在的目录。在拖动文件的情况下它:坦率地说, GetCurrentDirectory()在很大程度上是未定义的,但第一个文件的目录并不合理。

When you drop a file on to the executable, it will run it from the location of the file you've dropped. 将文件拖放到可执行文件时,它将从您删除的文件的位置运行它。 For example if you drop C:\\Path-To-File\\File.txt on to C:\\Program\\ApplicationA.exe it's like you've done the following from a command prompt: 例如,如果将C:\\ Path-To-File \\ File.txt放到C:\\ Program \\ ApplicationA.exe上,就像从命令提示符处完成以下操作一样:

cd C:\Path-To-File
C:\Program\ApplicationA.exe C:\Path-To-File\File.txt

When you run the application manually (in your example above) you've got control over the directory it's running from which is why it matches what you'd expect. 当您手动运行应用程序时(在上面的示例中),您可以控制它正在运行的目录,这就是它与您期望的匹配的原因。

I bet the problem is the 'Current Directory' property that is different. 我打赌问题是'当前目录'属性是不同的。

Current directory gives you the current working directory and not where the executable is. 当前目录为您提供当前工作目录,而不是可执行文件的位置。

When you drag&drop the current directory is set to the source (the drag source) and not the drop target 拖放时,当前目录设置为源(拖动源)而不是放置目标

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

相关问题 Directory.GetCurrentDirectory - Directory.GetCurrentDirectory Directory.GetCurrentDirectory()不能在linux上运行? - Directory.GetCurrentDirectory() not working on linux? 将转义序列添加到Directory.GetCurrentDirectory() - adding escape sequence to Directory.GetCurrentDirectory() WPF设置中的默认值为“ Directory.GetCurrentDirectory”。 - Default value in WPF Settings as `Directory.GetCurrentDirectory` 无法将文件写入 Directory.GetCurrentDirectory() 中的路径 - Unable to write file to path in Directory.GetCurrentDirectory() 升级到 VS 2017 15.6.0,现在 Directory.GetCurrentDirectory() 返回 \bin\Debug\netcoreapp2.0 - Upgraded to VS 2017 15.6.0 and now Directory.GetCurrentDirectory() returns \bin\Debug\netcoreapp2.0 Directory.GetCurrentDirectory() 方法在不同类型的 C# 项目中给出不同的路径 - Directory.GetCurrentDirectory() method gives different path in different type of C# project C#Directory.GetCurrentDirectory()返回带有NUnit的system32 - C# Directory.GetCurrentDirectory() returning system32 with NUnit C#:从浏览器启动时的Directory.getCurrentDirectory() - C#: Directory.getCurrentDirectory() when launching from Browser Environment.CurrentDirectory 和 Directory.GetCurrentDirectory 有什么区别? - What is the difference between Environment.CurrentDirectory and Directory.GetCurrentDirectory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM