getting current drive letter.

Hi folks:

How can I get the current drive letter using .NET API. thanks in advance.

rxrazaAsked:
Who is Participating?
 
sumixCommented:

"current drive letter" means indeed the drive you're running your application from, but Application.StartupPath string doesn't return current directory, it returns the path of the executable. For current drive/directory you must use CurrentDirectory property (static) of Environment class, something like this:
          DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
          MessageBox.Show(di.Root.ToString()); // displays the drive letter

Sumix
                  
0
 
YurichCommented:
not very clear question... what do you mean "current drive letter"? - the drive you're running your program from?

that's how you can get the list of your logical drives in the system:

string[] drives = System.Environment.GetLogicalDrives();


that's how you can get the drive you're running your application from:

string ex_drive = Application.StartupPath;
string[] split = ex_drive.Split( '\\' );
ex_drive = split[ 0 ];

and that's how you can get the drive your operating system is installed on:

string os_drive = System.Environment.GetFolderPath( System.Environment.SpecialFolder.System );
string[] split = os_drive.Split( '\\' );
os_drive = split[ 0 ];

regards,
Yurich
0
 
YurichCommented:
2 sumix,
so, the way i showed doesn't give you a drive you're running your application on??
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
sumixCommented:

No, for example, if your exe is in a folder 'C:\Temp\MyApp.exe' and you start it from D: drive (check it on a CommandPrompt Window) like

  D:>C:\Temp\MyApp

    Application.StartupPath will have value 'C:\Temp' and you will obtain value 'C:' for current drive instead of 'D:'
0
 
YurichCommented:
ok, i'm convinced ;) good one
0
 
Diego PazosCommented:
DirectoryInfo D=new DirectoryInfo(".");
Console.WriteLine(D.Root.Name.Replace("\\",""));
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.