Getting "My Documents" or %APPDATA% path

How do I get the My Documents folder as an FString? Or how to convert %APPDATA% to the real path?

I tried using SHGetFolderPath as described here, but got lots of compile errors about DWORD.

I also tried this,

		FString FullPath = TEXT("%APPDATA%\\ShooterGame\\");
		FullPath += FileName;
// .....
		bSuccess = FFileHelper::SaveArrayToFile(ToBinary, *FullPath);

But, even though SaveArrayToFile returns True, when I check the folder C:\Users\Jefferson\AppData\Roaming\ShooterGame, it is empty.

So how could I get a good path to store saved games?

You want to use one of our cross platform functions.

Try FPaths::UserSavedDir()

It should give you what you want, while there, check out the other dir access functions as there are quite a few of them.

Thanks Josh. However UserSavedDir() is private. Why?

I used GameSavedDir() instead. It currently points to Projects/ShooterGame/Saved, so I wonder, will it point to the game folder on a packaged (release, final) game, or to My Documents?

Oh right, sorry, I went too fast with my answer.

That function supports the “user dir” for all other functions that may want to write to that directory structure (saved games, logs, screenshots, etc)

You are using the right function, look at ShouldSaveToUserDir()

It should write to your documents directory when “installed” (ie shipping) or when you tell it to via the cmd line param -SaveToUserDir

Ok cool! Using the -SaveToUserDir switch now points to my documents folder, D:/Documentos/ShooterGame/UE4-Rocket/Beta6/ShooterGame/Saved/, when using GameSavedDir() (I have changed my docs folder location and Rocket accurately found it). Thanks!

SHGetKnownFolderPath is probably the function you want to use, SHGetFolderPath has been deprecated for a while now. That said, it should work - so what errors were you getting?

Doesn’t works too. I get these errors (same as before):

1>C:\Program Files (x86)\Windows Kits\8.0\include\shared\rpcasync.h(114): error C2872: 'DWORD' : ambiguous symbol
1>          could be 'C:\Program Files (x86)\Windows Kits\8.0\include\shared\minwindef.h(156) : unsigned long DWORD'
1>          or       'C:\Rocket\Engine\Source\Runtime\Core\Public\Core.h(514) : DoNotUseOldUE4Type::DWORD'
1>C:\Program Files (x86)\Windows Kits\8.0\include\shared\rpcasync.h(126): error C2872: 'UINT' : ambiguous symbol
1>          could be 'C:\Program Files (x86)\Windows Kits\8.0\include\shared\minwindef.h(177) : unsigned int UINT'
1>          or       'C:\Rocket\Engine\Source\Runtime\Core\Public\Core.h(513) : DoNotUseOldUE4Type::UINT'
1>C:\Program Files (x86)\Windows Kits\8.0\include\shared\rpcasync.h(384): error C2872: 'DWORD' : ambiguous symbol
1>          could be 'C:\Program Files (x86)\Windows Kits\8.0\include\shared\minwindef.h(156) : unsigned long DWORD'
1>          or       'C:\Rocket\Engine\Source\Runtime\Core\Public\Core.h(514) : DoNotUseOldUE4Type::DWORD'
1>C:\Program Files (x86)\Windows Kits\8.0\include\shared\rpcasync.h(396): error C2872: 'DWORD' : ambiguous symbol
1>          could be 'C:\Program Files (x86)\Windows Kits\8.0\include\shared\minwindef.h(156) : unsigned long DWORD'
1>          or       'C:\Rocket\Engine\Source\Runtime\Core\Public\Core.h(514) : DoNotUseOldUE4Type::DWORD'
1>C:\Program Files (x86)\Windows Kits\8.0\include\shared\wtypesbase.h(145): error C2872: 'DWORD' : ambiguous symbol
1>          could be 'C:\Program Files (x86)\Windows Kits\8.0\include\shared\minwindef.h(156) : unsigned long DWORD'
1>          or       'C:\Rocket\Engine\Source\Runtime\Core\Public\Core.h(514) : DoNotUseOldUE4Type::DWORD'
1>C:\Program Files (x86)\Windows Kits\8.0\include\shared\wtypesbase.h(272): error C2872: 'DWORD' : ambiguous symbol
1>          could be 'C:\Program Files (x86)\Windows Kits\8.0\include\shared\minwindef.h(156) : unsigned long DWORD'

and so on until it halts at the 100th error.

Here’s the full code:

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "ShooterGame.h"
//#include 
//#include 
#include 

class FSaveGameSystem
{
public:
		
	static bool SaveGame(const FString& FileName)
	{
		FString FullPath = TEXT("%APPDATA%\\ShooterGame\\");
		FullPath += FileName;
		CHAR my_documents[MAX_PATH];
		HRESULT result = SHGetKnownFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, my_documents);
		bool bSuccess = false;
		FBufferArchive ToBinary;
		int32 x = 12312;
		float y = 25234.f;
		ToBinary << x;
		ToBinary << y;

		bSuccess = FFileHelper::SaveArrayToFile(ToBinary, *FullPath);
		ToBinary.FlushCache();
		ToBinary.Empty();

		return bSuccess;
	} 	
};

For any one looking for this now you can also get the “My Documents” path from the FPlatformProcess::UserDir() method.