2

I am writing some Selenium WebDriver and AutoIt Java Tests using Chrome. When I download a file in Chrome, it automatically downloads it to the Downloads folder on my computer. I am sharing this code project with multiple other users on a repository, and so I do not want to have my Downloads folder hard coded in with my specific username in it.

What I mean by this is let's say the user name on my computer is "Eamon." The downloads folder on my machine would be "C:\Users\Eamon\Downloads," but lets say my friend Mark downloads the project from the shared repository, his downloads folder will be located at "C:\Users\Mark\Downloads," yet as he pulls from the repository to see my updated code, the download location will still be hard coded as ""C:\Users\Eamon\Downloads" which will result in a "Folder does not exist" error.

Is there a way in java to access the generic Downloads folder on a machine that would change based on who the user of the machine is? This would help my test a lot.

Eamon Bacardi
  • 23
  • 1
  • 5

2 Answers2

5

For a Windows machine :

new File("C:/Users/" + System.getProperty("user.name") + "/Downloads/");

Could use similar snippets for any other operating system.

Dici
  • 25,226
  • 7
  • 41
  • 82
  • What about non-english windows installations (and not only windows, gnu/linux folders can also be internationalized)? There directory wouldn't be called that way. – Al.G. Jan 09 '17 at 10:39
  • The question was more around how to inject the username though. To answer you, I'm actually pretty sure this path is still valid in a non-english configuration, at least it was the case for me when I had a Windows configured in french. – Dici Jan 10 '17 at 04:21
  • Apparently not the case at least for **Program Files**: http://www.samlogic.net/articles/program-files-folder-different-languages.htm. It seems to imply that you can rely on environment variables containing the localized name of these folders – Dici Jan 10 '17 at 04:25
2

String myHomePath= System.getProperty("user.home");

File file = new File(myHomePath+"/Downloads/" + fileName);

Note: give file name with proper extension e.g foo.txt

reshma
  • 641
  • 1
  • 11
  • 18