Detect System Drive

Is there a class/method in java to detect the system drive of the user's computer.  This only has to work for Windows based PCs.  Basically I just need to know the letter (i.e. C:)

Thanks
LVL 18
mgcITAsked:
Who is Participating?
 
WebstormCommented:
It's the same as your profile drive, so :

    String s=System.getProperty("user.home");
    // -> something like C:\Documents and Settings\yourname
0
 
WebstormCommented:
File[] fdrives=File.listRoots();
0
 
WebstormCommented:
--> you will get an array of File, for example:
   A:\ C:\  D:\
(A:\ maybe only if you have inserted a disk in the drive)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
WebstormCommented:
just tested :  A:\ is listed even without disk in drive
0
 
mgcITAuthor Commented:
that's just a list of drives on the system though right?  How do I detect the Windows System drive?
0
 
WebstormCommented:
only the drive letter :
   s.substring(0,2);
0
 
WebstormCommented:
Otherwise, you can use environment variables : CommonProgramFiles, HOMEDRIVE, HOMEPATH, ProgramFiles, SystemDrive, SystemRoot, TEMP, windir

  -  with Java 1.5.0   System.getenv("SystemDrive");
  -  older versions : you have to use command line arguments:
               java ... -DSystemDrive=%SystemDrive% <yourclass>
               System.getProperty("SystemDrive");


0
 
objectsCommented:
You coulkd use listRooots() and then loop thru them looking for which one has a Windows directory
0
 
WebstormCommented:
>> You coulkd use listRooots() and then loop thru them looking for which one has a Windows directory
You have to check "WinNT" and "Windows" directories.
Cannot work when you have multiple versions of Windows installed.
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.