Paul Bottas

Paul Bottas

  • NA
  • 1
  • 0

How do I get/switch to the current directory on another drive??

Mar 18 2009 10:03 PM

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

 


Answers (1)