问题
What is the right place to store program data files which are the same for every user but have to be writeable for the program? What would be the equivalent location on MS Windows XP? I have read that C:\ProgramData is not writeable after installation by normal users. Is that true? How can I retrieve that directory programmatically using the Platform SDK?
回答1:
SHGetFolderPath() with CSIDL of CSIDL_COMMON_APPDATA.
Read more at http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx
If you need the path in a batch file, you can also use the %ALLUSERSPROFILE% environment variable.
回答2:
There is a great summary of the different options here: http://blogs.msdn.com/cjacks/archive/2008/02/05/where-should-i-write-program-data-instead-of-program-files.aspx
Where Should I Write Program Data Instead of Program Files?
A common application code update is this: "my application used to write files to program files. It felt like as good a place to put it as any other. It had my application's name on it already, and because my users were admins, it worked fine. But now I see that this may not be as great a place to stick things as I once thought, because with UAC even Administrators run with standard user-like privileges most of the time. So, where should I put my files instead?"
回答3:
Actually SHGetFolderPath is deprecated.
SHGetKnownFolderPath should be used instead.
回答4:
You can use:
CString strPath;
::SHGetSpecialFolderPath(NULL, strPath.GetBuffer(1024), CSIDL_COMMON_APPDATA, FALSE);
回答5:
See Raymond Chen's article on this specific question.
In short you're asking for a security hole.
来源:https://stackoverflow.com/questions/131666/vista-and-programdata