当您构建和调试 Windows 应用商店应用时,Visual Studio 的部署会安装应用并运行它,就像它是通过应用商店部署或旁加载一样。
与用户帐户控制器一样,您的应用程序应创建任何初始版本的 AppData 文件和所需的子目录(如果它们不存在)。
见File access and permissions (Windows Runtime apps)
编辑:对于 Windows 应用商店应用,您可以从 Windows::Storage::ApplicationData 和 Current->LocalFolder、Current->RoamingFolder 或 Current->TemporaryFolder 属性中获取可以使用的目录的路径。
对于 Windows 桌面应用程序,您使用了 Windows 2000/XP SHGetFolderPath Win32 API 或 Windows Vista 时代 IKnownFolder COM API(具有更易于使用的包装器 SHGetKnownFolderPath)。 Dual-use Coding Techniques for Games 帖子展示了编写这两个版本的一些示例。
#include <wrl\client.h>
using Microsoft::WRL::ComPtr;
void GetApplicationDataDirectory(wchar_t* dir, size_t maxsize)
{
if (!maxsize) return;
*dir = 0;
#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
// You can use the Win32 SHGetKnownFolderPath as well which is just
// a wrapper that does the same thing.
// On Windows XP, you use the older SHGetFolderPath function with
// different constants that have the same meaning.
ComPtr<IKnownFolderManager> mgr;
HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager,
nullptr, CLSCTX_INPROC_SERVER, IID_IKnownFolderManager, (LPVOID*) &mgr);
if (SUCCEEDED(hr))
{
ComPtr<IKnownFolder> folder;
hr = mgr->GetFolder(FOLDERID_LocalAppData, &folder);
if (SUCCEEDED(hr))
{
LPWSTR szPath = 0;
hr = folder->GetPath(0, &szPath);
if (SUCCEEDED(hr))
{
// A big different with Windows desktop apps is here.
// With Windows Store apps, your appdata directory is
// isolated per-user and per-app. In Windows desktop,
// it is only isolated per-user so you have to create
// your own unique subdir and other Windows desktop apps
// can mess with your data
wcscpy_s(dir, maxsize, szPath);
wcscat_s(dir, maxsize, L”\\MyUniqueApplicationName”);
CreateDirectory(dir, nullptr);
CoTaskMemFree(szPath);
}
}
}
#else // Windows Store WinRT app
auto folder = Windows::Storage::ApplicationData::Current
->LocalFolder;
wcscpy_s(dir, maxsize, folder->Path->Data());
#endif
}