Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

getting current drive letter.

Posted on 2005-05-12
6
Medium Priority
?
368 Views
Last Modified: 2011-09-20
Hi folks:

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

0
Comment
Question by:rxraza
  • 3
  • 2
6 Comments
 
LVL 21

Expert Comment

by:Yurich
ID: 13992273
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
 
LVL 12

Accepted Solution

by:
sumix earned 200 total points
ID: 13993489

"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
 
LVL 21

Expert Comment

by:Yurich
ID: 13993623
2 sumix,
so, the way i showed doesn't give you a drive you're running your application on??
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:sumix
ID: 13993756

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
 
LVL 21

Expert Comment

by:Yurich
ID: 13993789
ok, i'm convinced ;) good one
0
 
LVL 3

Expert Comment

by:Diego Pazos
ID: 13997599
DirectoryInfo D=new DirectoryInfo(".");
Console.WriteLine(D.Root.Name.Replace("\\",""));
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question