Link to home
Start Free TrialLog in
Avatar of Maranellas
Maranellas

asked on

How to detect system drive using JAVA

As the topic says.I need short function to detect system drive letter.
Avatar of ioanton
ioanton

You can use:

File[] roots = File.listRoots();

to get the list of drives on your system
Avatar of Maranellas

ASKER

I don't need to get list of drives.I need to get main drive where windows installed.
Avatar of Gurvinder Pal Singh
try the following code

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

see the following link for reference
http://www.bigresource.com/Tracker/Track-vb-NkqegKV5n3/
http://leepoint.net/notes-java/io/30properties_and_preferences/40sysprops/10sysprop.html
Another approach:

System.out.println(System.getProperty("user.home").charAt(0));

Assuming the user home directory is always an the windows drive.
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
ASKER CERTIFIED SOLUTION
Avatar of ioanton
ioanton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial