getting current drive letter.

Posted on 2005-05-12
Last Modified: 2011-09-20
Hi folks:

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

Question by:rxraza
    LVL 21

    Expert Comment

    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 ];

    LVL 12

    Accepted Solution


    "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

    LVL 21

    Expert Comment

    2 sumix,
    so, the way i showed doesn't give you a drive you're running your application on??
    LVL 12

    Expert Comment


    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


        Application.StartupPath will have value 'C:\Temp' and you will obtain value 'C:' for current drive instead of 'D:'
    LVL 21

    Expert Comment

    ok, i'm convinced ;) good one
    LVL 3

    Expert Comment

    DirectoryInfo D=new DirectoryInfo(".");

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now