Given:
I'm programing in C#, .NET 3.5, VS2005, WinXP pro SP3 (VS version & OS shouldn't matter)
Let's say that I've got more than one drive. (duh!) drives C: and D:
My current directory is: C:\SomeDirectoryOnDrvC\SomeSubdirectory
Question(s):
1) How do I find out what the current directory is on drive D: WITHOUT CHANGING THE CURRENT DRIVE to drive D:?
2) How do I change to drive D: preserving the current directory on that drive? (I know I can use the Directory class method .SetCurrentDirectory(@"D:") to change to a directory on drive D: (or any given drive). But it doesn't preserve what the current directory is currenty set to on that drive. No, I'm talking about something like the when you have a command window open and you enter "D:" It change you to drive D: to what ever the current directory is set to there. You can then do a "CD" comannd to change directories. If you then enter "C:", you're back to the current directory on drive C: If you then enter "D:", it changes you back to drive D: into what directory you just CD'd into?
Take the follow block of code
string curDir;curDir =
Directory.GetCurrentDirectory(); Console.WriteLine("the current directory is {0}", curDir); Console.WriteLine(@"changing to C:\Documents and Settings\PMBottas"); Directory.SetCurrentDirectory(@"C:\Documents and Settings\PMBottas");curDir =
Directory.GetCurrentDirectory(); Console.WriteLine("the current directory on drive C: is {0}\n", curDir); Directory.SetCurrentDirectory(@"D:");curDir =
Directory.GetCurrentDirectory(); Console.WriteLine("the current directory on drive D: is {0}", curDir); Console.WriteLine(@"changing to D:\Documents\All Lessons"); Directory.SetCurrentDirectory(@"D:\Documents\All Lessons");curDir =
Directory.GetCurrentDirectory(); Console.WriteLine("the current directory on drive D: is {0}\n", curDir); Console.WriteLine("changing back to C:"); Directory.SetCurrentDirectory(@"C:");curDir =
Directory.GetCurrentDirectory(); Console.WriteLine("the current directory on C: is {0}", curDir); Console.WriteLine("changing back to D:"); Directory.SetCurrentDirectory(@"D:");curDir =
Directory.GetCurrentDirectory(); Console.WriteLine("the current directory on drive d: is {0}\n", curDir); It's output is:the current directory is C:\Documents and Settings\PMBottas\My Documents\Visual Studio 2005\Projects\ChDrv\bin\Debug
changing to C:\Documents and Settings\PMBottas
the current directory on drive C: is C:\Documents and Settings\PMBottas
the current directory on drive D: is D:\
changing to D:\Documents\All Lessons
the current directory on drive D: is D:\Documents\All Lessons
changing back to C:
the current directory on C: is C:\Program Files\Microsoft Visual Studio 8\Common7\IDE
changing back to D:
the current directory on drive d: is D:\
What gives and why do I end up in the root when I switch back to D: and in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE when I switch back to C:
Any help would be greatly appreciated
Thanks - Paul
Vimal KandasamyPosted Mar 19, 2009, 3:57 AM
up to my knowledge
1.we can get root directory of a drive or any folder without changing into it.but we cant get current directory.
2.for me,
while change from D to C.it shows current directory as "c:\" only, not as "C:\Program Files\Microsoft Visual Studio 8\Common7\IDE". [it will show the directory, from where the programs start its execution i think so.]
Directory.SetCurrentDirectory() method will set a directory as a current directory for the whole.it wont set it to that particular drive.
and take a look at code
Directory.SetCurrentDirectory("c:\\a");
Directory.SetCurrentDirectory("d:\\b");
in the 1st line we set "c:\a" as current directory.after execution of 2nd line current directory info is changed into "d:\b".... it does not care about "c:\a"
so currentDirectory info can be set only once. and it won't keep the track of previous settings and each drive settings i think so.
i may not be exact.
if you find right solution plz share it with us.