aspose file tools*
The moose likes Java in General and the fly likes Find windows special folders Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Find windows special folders" Watch "Find windows special folders" New topic
Author

Find windows special folders

Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
Hi all,

I need to be able to find the location of the windows 7/vista "saved games" folder and I want to do it properly so my app will not have localization problems. I have used System.getenv and System.getProperty to find the locations of every other folder that I need but neither are able to get this folder. I defiantly need to find it somehow, if it's not possible in java does anyone know a way to get it using some kind of script called by a java app or any other technique?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10255
    
    8

I am not a Windoze user, so I don't know what this "saved games" folder is.
Is it a standard folder created by the OS? Does it have the same location for all users? Something like user.home/saved games?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
In English versions of windows 7 its located in the personal folder, "C:\Users\[USERNAME]\Saved Games". I can get the path to the personal folder no problem but the trouble is it's possible to change the location of this folder to anything the user wants and it will have a different name in other languages.

While typing this I thought of checking the registry, upon looking I found the file paths for all windows "special folders" stored under [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]. I found this previous topic Read the Windows registry from java... which covers reading the registry in JAVA so hopefully that's problem solved. I'll try it and post results in the morning.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4529
    
    5

Did you look here?


luck, db
There are no new questions, but there may be new answers.
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
If you google anything with the words "saved games" included you find a million and one people saying "Where do I find the saved games for (insert game here)". This windows special folder is not as easy to find as the other windows special folders in java either so searching for how to find another one also yielded me no usable results.


I've edited this post to be less rude. I apologize for before, I was tired and not thinking.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Colin, please BeNice. Darryl's Google search did not specify "save games" but "windows special folders" - in other words, not just the save games folder but all special folders. Using that search (minus the "java") I found this: http://en.wikipedia.org/wiki/Special_Folders#List_of_special_folders

And if you want to go native and are sure you're aiming for a Windows Vista/7 only user base, check out http://msdn.microsoft.com/en-us/library/bb762188(v=vs.85).aspx. Before Windows Vista/7 there was no system saved games folder yet.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

If you search for the folder in the registry, you're tied to Windows already. There should be a Windows function to obtain any of the special folders (I assume identified by some integer constant), which probably would be preferable, as it would save you from interpreting the registry (there are endless possible setups of Windows, and in some of them the value could be not there, for example). You might search MSDN for such function and call it via JNA.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Martin Vajsar wrote:There should be a Windows function to obtain any of the special folders (I assume identified by some integer constant), which probably would be preferable, as it would save you from interpreting the registry (there are endless possible setups of Windows, and in some of them the value could be not there, for example). You might search MSDN for such function and call it via JNA.

There are a few: SHGetSpecialFolder and its successor SHGetFolderPath are available since Windows 2000, and they both use the CSIDL constants. However, the saved games folder wasn't a special folder until Vista. It introduced SHGetKnownFolderPath (which I linked to earlier) which uses KNOWNFOLDERID instead of CSIDL. This type adds a few more special folders, one of which is the save games folder.
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
Sorry if I was rude last night. long day coupled with lack of sleep and pet cat and dog chasing each other all day must have left me short tempered
I'll edit the post

I know the folder is only on vista/7, my app is multi platrom as It will run on xp/vista/7 and mac as well. I check for the os version and was going to check the registry behind an if statement only if it were windows vista or 7. I already use a different method in my code to check the special folders for xp, that part of my app is finished.

I had already found an msdn page on special folders here but I thought that information was only accessible through .net or C# and if so I thought checking the registry would be better as it allows me to stay pure java.

This registry key should always be there for windows 7 and vista as it is a windows system folder now.

Which approach is better registry reading vs JNA? Never heard of JNA before now...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You don't need .NET or C#; regular C in JNI is also possible. JNA is similar to JNI, except it lets you write the native code in Java equivalents. It doesn't require you to write and compile a DLL (or .so for Unix) file.

Accessing the registry is also possible of course. It wouldn't even surprise me that the registry and the return value of SHGetKnownFolderPath are related.
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
looks like I need JNA to read the registry as well, shame, it makes my apps file size 10 times bigger than it was including the library...

Since this is the case I'll just use SHGetKnownFolderPath. Is there an example anywhere of JNA using SHGetKnownFolderPath? anywhere? I have not been programming very long and am only comfortable coding in java atm so I'm not sure how to implement this.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4529
    
    5

Sorry I couldn't get back to this earlier, I have WinXP at work and wanted to test on my Win7 machine at home before I posted.

With a bit more googling, I found the GUID for the "Saved Games" special folder is documented.
http://msdn.microsoft.com/en-us/library/dd378457(v=vs.85).aspx

This can be used to look up the location in a locale independent way.

edit Fixed the link -- the forum's parsing had broken it at the closing parenthesis
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
That is fantastic, I can use this method to find any windows special folder without using JNA

Thanks for all the help people, tis much appreciated.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

When Runtime.exec() won't.
Colin McTaggart
Ranch Hand

Joined: Feb 01, 2011
Posts: 46
Just thought I'd mention double fantastic. I was also having trouble finding the install directories of several pieces of non windows software as well. I changed the registry key this solution points to the the keys for that software and it works. Not only that but I pointed it at a bunch of random registry keys and it gave me their values as well

Edit just read the linked article about this method, from what I understood of it my use of this method shouldn't cause any problem but in case it does I should catch all errors and if this method fails to find the directory I can prompt the user to input the directory in a dialogue if its needed.

Thanks again for all the help
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Find windows special folders