c# why when the path is "C:" the directoryInfo takes me to the application folder?
Asked Answered
V

4

5

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!

Valiancy answered 3/2, 2012 at 14:39 Comment(3)
Because that's how it's worked since 1983 and it would be confusing to change it now!Corrinnecorrival
Try DirectoryInfo dir = new DirectoryInfo("c:\\"); or DirectoryInfo dir = new DirectoryInfo(@"c:\");.Omsk
doesn't work for my application my application i just edit my question .. check it outValiancy
W
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(); 
    } 
Whipping answered 3/2, 2012 at 14:45 Comment(6)
you are the only who got my point .. so i guess that this's the only way to do right thanks alotValiancy
you are the only who got my point .. so i guess that this's the only way to do right thanks alotValiancy
@MurHafSoz: If lots of people are failing to understand you, you should consider explaining yourself more clearly.Molly
i didn't asked for an explanation because i've already know my problem .. i asked for a solution .. and he's the only one who understoodValiancy
@MurHafSoz: I assure you that you did ask for an explanation. Your question begins with "why does", which is a request for an explanation. If you wanted to know how to do something then your question should have begun with "how do I".Corrinnecorrival
What's up with the substring and the equals? Why not just EndsWith?Polygon
M
15

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...

Molly answered 3/2, 2012 at 14:41 Comment(0)
P
6

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.

Patterson answered 3/2, 2012 at 14:40 Comment(1)
that means you should be using `C:` which specifies the path on the disk as wellJoselow
W
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(); 
    } 
Whipping answered 3/2, 2012 at 14:45 Comment(6)
you are the only who got my point .. so i guess that this's the only way to do right thanks alotValiancy
you are the only who got my point .. so i guess that this's the only way to do right thanks alotValiancy
@MurHafSoz: If lots of people are failing to understand you, you should consider explaining yourself more clearly.Molly
i didn't asked for an explanation because i've already know my problem .. i asked for a solution .. and he's the only one who understoodValiancy
@MurHafSoz: I assure you that you did ask for an explanation. Your question begins with "why does", which is a request for an explanation. If you wanted to know how to do something then your question should have begun with "how do I".Corrinnecorrival
What's up with the substring and the equals? Why not just EndsWith?Polygon
C
0

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..

Cavell answered 3/2, 2012 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.