[英]Windows 8: How to read the user documents folder path programmatically (using c )?
To access the files in the app directory, I am using the following line of code: 要访问app目录中的文件,我使用以下代码行:
std::wstring wpath = Windows::Storage::ApplicationData::Current->LocalFolder->Path->Data();
Similar to the above code, how can I get the documents folder path? 与以上代码类似,如何获取文件文件夹路径? Please suggest a solution that works in Metro app. 请提出适用于Metro应用的解决方案。
I found this answer here on Stack Overflow 我发现这个答案在这里对堆栈溢出
Because DocumentsLibrary is a virtual location representing a collection of different locations and files, it will not have a path: 因为DocumentsLibrary是一个虚拟位置,代表不同位置和文件的集合,所以它没有路径:
Libraries, a concept introduced in Windows 7, allow users to view related user content from a single location. 库是Windows 7中引入的一个概念,允许用户从一个位置查看相关的用户内容。 Because the library is a virtual location, folders that are added to or removed from a library continue to exist in their original locations as well. 因为该库是一个虚拟位置,所以添加到库或从库中删除的文件夹仍继续存在于其原始位置。
In addition, the MSDN documentation for StorageFolder.Path states: 此外,StorageFolder.Path的MSDN文档指出:
Do not rely on this property to access a folder because some folders may not have file-system paths. 不要依赖此属性来访问文件夹,因为某些文件夹可能没有文件系统路径。 For example if the folder is a file group, or is backed by a URI, or was picked using the file picker, the folder is not guaranteed to have a file-system path. 例如,如果该文件夹是文件组,或者由URI支持,或者是使用文件选择器选择的,则不能保证该文件夹具有文件系统路径。
but I suggest you to look at this code, search for (line 796): 但我建议您查看以下代码,进行搜索(第796行):
case Win_Documents:
{
return GetLibrarySaveToPath(CSIDL_MYDOCUMENTS,
FOLDERID_DocumentsLibrary,
aFile);
}
your problem seems to be resolved by this piece of code: 您的问题似乎可以通过以下代码解决:
/*
* Check to see if we're on Win7 and up, and if so, returns the default
* save-to location for the Windows Library passed in through aFolderId.
* Otherwise falls back on pre-win7 GetWindowsFolder.
*/
static nsresult
GetLibrarySaveToPath(int aFallbackFolderId, REFKNOWNFOLDERID aFolderId,
nsIFile** aFile)
{
// Skip off checking for library support if the os is Vista or lower.
DWORD dwVersion = GetVersion();
if ((DWORD)(LOBYTE(LOWORD(dwVersion))) < 6 ||
((DWORD)(LOBYTE(LOWORD(dwVersion))) == 6 &&
(DWORD)(HIBYTE(LOWORD(dwVersion))) == 0))
return GetWindowsFolder(aFallbackFolderId, aFile);
nsRefPtr<IShellLibrary> shellLib;
nsRefPtr<IShellItem> savePath;
HRESULT hr =
SHLoadLibraryFromKnownFolder(aFolderId, STGM_READ,
IID_IShellLibrary, getter_AddRefs(shellLib));
if (shellLib &&
SUCCEEDED(shellLib->GetDefaultSaveFolder(DSFT_DETECT, IID_IShellItem,
getter_AddRefs(savePath)))) {
PRUnichar* str = nullptr;
if (SUCCEEDED(savePath->GetDisplayName(SIGDN_FILESYSPATH, &str))) {
nsAutoString path;
path.Assign(str);
path.AppendLiteral("\\");
nsresult rv =
NS_NewLocalFile(path, false, aFile);
CoTaskMemFree(str);
return rv;
}
}
return GetWindowsFolder(aFallbackFolderId, aFile);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.