• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

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
0
mgcIT
Asked:
mgcIT
  • 7
2 Solutions
 
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
 
WebstormCommented:
just tested :  A:\ is listed even without disk in drive
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mgcITAuthor Commented:
that's just a list of drives on the system though right?  How do I detect the Windows System drive?
0
 
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:
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

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.

  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now