Directory.GetCurrentDirectory()返回基于命令行参数的不同结果 [英] Directory.GetCurrentDirectory() returns different results based on command line arguments

查看:266
本文介绍了Directory.GetCurrentDirectory()返回基于命令行参数的不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能解释为什么 Directory.GetCurrentDirectory()返回基于我如何通过我的命令行参数的应用程序不同的结果(用的args VS拖动文件夹在应用程序运行。 exe文件)

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)

要直接进入它认为这片code:

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


如果你建立并拖动文件或文件夹在应用程序运行这个程序,你得到不同的结果。而不是返回预期的结果,而不是Directory.GetCurrentDirectory()的返回路径,你已经拖过应用程序的第一个文件。


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.


我目前得到了一个解决此问题不过,我很希望了解为什么发生这种情况。


I've currently got a work around for this issue however, I am keen to understand why this is happening.

其它信息:

  • 在.NET 4.5
  • 的Windows 2012R2(虚拟机)
  • 在计算机上完全管理员权限

希望有人能提供一些见解。

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.

这是不可以我从 GetCurrentDirectory预期()。当前目录是调用上下文的功能,而不是应用程序。如果我通过一个完整或相对路径运行可执行文件(而不仅仅是 foo.exe的),我期待 GetCurrentDirectory()返回目录的我在的 - 而不是应用程序所在的目录中拖动文件在它的情况:坦率地说, GetCurrentDirectory()在很大程度上是不确定的,但第一个文件的目录是没有道理的。

That is not what I expect from 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.

这篇关于Directory.GetCurrentDirectory()返回基于命令行参数的不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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