Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error accessing file from "My Documents" for Windows Xp and Windows 7

Sir, i am working in a java application.On that application i have to access files from "My Documents". The problem is coming with windows version when i am using windows 7, it can be accessed as "Documents" folder but for windows XP it is "My Documents".

I am writing following code to access files from "Documents" folder in windows 7 .

 public static void main(String[] arr)
 {
     try
     {
         String source = System.getProperty("user.home")+ File.separator + "Documents";
         File[] Files = new File(source).listFiles();
         System.out.println(Files.length);
     }
     catch(Exception ex)
     {
         ex.printStackTrace();
     }
 }

and for Windows Xp

 public static void main(String[] arr)
 {
     try
     {
         String source = System.getProperty("user.home")+ File.separator + "My Documents";
         File[] Files = new File(source).listFiles();
         System.out.println(Files.length);
     }
     catch(Exception ex)
     {
         ex.printStackTrace();
     }
 }

Please could you suggest me a generic method, which can be applied for all the versions of Windows?

like image 415
Toman Avatar asked Aug 31 '10 12:08

Toman


1 Answers

You can check for the operating system version and then use that to map the proper file name.

like image 195
Shamim Hafiz - MSFT Avatar answered Oct 14 '22 01:10

Shamim Hafiz - MSFT