c# why when the path is “C:” the directoryInfo tak

2020-03-22 07:34发布

Why when i give the path "c:" it changed me directly to application folder?

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

The output is the following:

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

But when I give @"c:\" it goes to disk c: despite that "d:" and @"d:\" takes to disk d:.

So I need a way to let "c:" takes to disk c:

Thanks in advance!

4条回答
Evening l夕情丶
2楼-- · 2020-03-22 07:46

Just "c:" means "the current directory on the C drive" whereas @"c:\" means "root of the C drive". This works the same way from a command prompt...

查看更多
Rolldiameter
3楼-- · 2020-03-22 07:49

C: is just the volume specifier, so it will change to your current path on that volume, which would be the working path of the application.

D: takes you to root simply because your current folder for that volume happens to be at root.

查看更多
Viruses.
4楼-- · 2020-03-22 07:55

enter image description here

Use the following

  static void Main(string[] args)
  {          
      DirectoryInfo dir = new DirectoryInfo(@"c:\");
      Console.WriteLine(dir.FullName);
      Console.ReadLine();
  }       

The Base Directory at the time when you do c: the application doesn't understand that so it returns the Directory of where the application was launched / run from.

Notice that dir = {.} if you would have passed in a literal directory path you would have gotten the expected results..

查看更多
Lonely孤独者°
5楼-- · 2020-03-22 08:01
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(); 
    } 
查看更多
登录 后发表回答