DirectoryInfo ends in weird result??

Neutrino

Member
Joined
Jun 14, 2003
Messages
21
Hi Frenz,

I created a Console Application in C:\ConsoleApp . I have a folder named "Messages" in my "D:\" . But when I try to use the DirectoryInfo Class to get the Root Directory it gave me erroneus results.

Code:

C#:
DirectoryInfo objDir = new DirectoryInfo("\\Messages);
string rootErrorDir = objDir.Root.ToString();
string rootErrDir = objDir.FullName.ToString();

Console.Write(rootErrorDir);
Console.Write(Environment.NewLine);
Console.Write(rootErrDir);
Console.Read();

When I executed the above code, it gave me the FullName as "C:\Messages" instead of "D:\Messages". Even the "Root" is given as "C:\".

Similarly just for error elimination purposes, I deleted the above folders et stuff. Now I created the folder "Messages" in my "C:\" and the same project with the same code in my D:\ (same as the above example except interchanged).

Its getting even weird now, it returns me the FullName as "D:\Messages" instead of "C:\Messages" and the Root as "D:\".

Can neone give me a suitable explanation for the above behaviour?

Amicalement,
Neutrino
 
Last edited by a moderator:
It appears to be working as expected. A root drive is not specified in the constructer of the DirectoryInfo object, and therefore the root will default to the current working directory.
 
What should I do if I want to do a search in my other drives if I am currently in my C:\ drive? In this case I am not sure about the availablitt of the other drives and it could also be a mapped drive on a network?

We can find the Logical Drives, but how do I search for a particular folder in another drive when I am currently no in that drive.
Can you please help out?

Amicalement,
Neutrino
 
Since you know how to enumerate the drives, just go through each one and use
Code:
If Directory.Exists(DriveLetter & "\Messages") Then
   The directory is on this drive
End If
 
Back
Top