c#为什么当路径为"C:"时,directoryInfo将我带到应用程序文件夹?

Mur*_*sli 5 c# path directoryinfo

为什么当我给路径"c:"时,它直接将我改为应用程序文件夹?

    static void Main(string[] args)
    {
        DirectoryInfo dir = new DirectoryInfo("c:");
        Console.WriteLine(dir.FullName);
        Console.ReadLine();
    }
Run Code Online (Sandbox Code Playgroud)

输出如下:

c:\ users ...\documents\visual studio 2010\projects\consoleApplication9\bin\debug

但是当我把@"c:\"它放到磁盘上时c: 尽管如此"d:"并将@"d:\"其带到磁盘上d:.

所以我需要一种让"c:"磁盘转发的方法c:

提前致谢!

Jon*_*eet 15

"c:"表示"C驱动器上的当前目录",而@"c:\""C驱动器的根"表示.这与命令提示符的工作方式相同...


Gaz*_*yer 6

C:只是卷说明符,因此它将更改为该卷上的当前路径,这将是应用程序的工作路径.

D:简单地将您带到root用户,因为该卷的当前文件夹恰好位于root用户.


小智 2

static void Main(string[] args) 
    { 
        string YourDir = "c:";

        if (!YourDir.Substring(YourDir.Length - 1, 1).Equals(@"\"))
            YourDir += @"\";
        DirectoryInfo dir = new DirectoryInfo(YourDir); 
        Console.WriteLine(dir.FullName); 
        Console.ReadLine(); 
    } 
Run Code Online (Sandbox Code Playgroud)

  • @MurHafSoz:我向你保证你确实要求解释。你的问题以“为什么”开头,这是要求解释的请求。如果你想知道如何做某事,那么你的问题应该以“我该怎么做”开始。 (7认同)
  • @MurHafSoz:如果很多人无法理解你,你应该考虑更清楚地解释自己。 (5认同)
  • 子字符串和等号怎么了?为什么不直接以 EndsWith 结尾呢? (2认同)