How to detect system drive using JAVA

Posted on 2009-12-25
Last Modified: 2013-12-14
As the topic says.I need short function to detect system drive letter.
Question by:Maranellas
    LVL 9

    Expert Comment

    You can use:

    File[] roots = File.listRoots();

    to get the list of drives on your system

    Author Comment

    I don't need to get list of drives.I need to get main drive where windows installed.
    LVL 40

    Expert Comment

    try the following code

    String systemDrive = System.getProperty("SystemDrive");

    see the following link for reference
    LVL 9

    Expert Comment

    Another approach:


    Assuming the user home directory is always an the windows drive.

    Author Comment

    okay when i try

    String systemDrive = System.getProperty("SystemDrive");

    i get NULL response

    and when i try System.out.println(System.getProperty("user.home").charAt(0));

    i get the following error

    found   : char
    required: java.lang.String
            private String drv = System.getProperty("user.home").charAt(0);

    but java.lang package is imported already
    LVL 9

    Accepted Solution

    Indeed, the returned value is of type char. But you can convert it to string using:


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from ( Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

    737 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

    21 Experts available now in Live!

    Get 1:1 Help Now