积极答复者
请问使用 FileOpenPicker 选取一个文件,能否推断出该文件夹

问题
-
请问,可以从用户选择文件,推导出这个文件夹吗?FileOpenPicker^ filePicker = ref new FileOpenPicker(); filePicker->SuggestedStartLocation = PickerLocationId::VideosLibrary; filePicker->FileTypeFilter->Clear(); filePicker->ViewMode = PickerViewMode::List | PickerViewMode::Thumbnail ; filePicker->FileTypeFilter->Append("*"); task<StorageFile^>(filePicker->PickSingleFileAsync()). then([this](StorageFile^ videoFile) { if (videoFile) {
//请注意,这个转换是错误的,崩溃。 StorageFolder^ sb = dynamic_cast<StorageFolder^>(videoFile->FolderRelativeId);
//我想用户选择文件之后,保存这个文件夹的令牌,以方便将来访问,注意:我不是保存这个文件的令牌。 fileToken = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList->Add(sb); } });
答案
-
对于文件夹来说Path最后的地方应该没有双斜杠把
James He
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已编辑 Jamles HezModerator 2013年8月7日 9:18 update
- 已标记为答案 李磊和韩梅梅2 2013年8月7日 9:21
全部回复
-
Hi 李磊和韩梅梅
这里有个问题,你的文件是通过FileOpenPicker选择的,所以获得的是对于这个文件而言的令牌,而非对于文件夹的令牌,所以我认为文件夹不能被推导出来。
另外这里有一个关于文件是否可以通过Path获取的讨论,我觉得会对你很有帮助 http://social.msdn.microsoft.com/Forums/windowsapps/en-US/46a3088d-8420-485b-8697-8b2dda0f1f19/winrt-information-access-is-not-allowed-to-the-provided-path。 因为Windows Store app是基于SandBox运行的,所以访问系统中的文件会被限制。
请知悉。
James He
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已编辑 Jamles HezModerator 2013年8月7日 4:22 update
-
的确是这样的,通过对文件夹的遍历可以获取对文件访问权限。
目前只有两种方式可以获取文件夹的访问权限,一种是在Package.appxmanifest中定义,不过这里面定义的一般都是预设的文件夹比如说Library或者Installation Folder之类的文件夹,还有一种就是通过FolderOpenPicker来给定权限。
我看你代码中写的是VideoFile,你可以保存在VideosLibrary里面。
James He
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
//用户选择文件 FileOpenPicker^ filePicker = ref new FileOpenPicker(); filePicker->SuggestedStartLocation = PickerLocationId::VideosLibrary; filePicker->FileTypeFilter->Clear(); filePicker->ViewMode = PickerViewMode::List | PickerViewMode::Thumbnail ; filePicker->FileTypeFilter->Append("*"); task<StorageFile^>(filePicker->PickSingleFileAsync()). then([this](StorageFile^ videoFile) { if (videoFile) { std::wstring strName(videoFile->Path->Data()); int nPos = strName.rfind(L"\\"); std::wstring str = strName.substr(0,nPos); Platform::String^ str2 = ref new String(str.c_str()); //str2 = "c:\\desktop\\viedo\\" 运行报错 “无效参数” task<StorageFolder^>(StorageFolder::GetFolderFromPathAsync(str2)).then([this](StorageFolder^ folder2) { if (folder2) { fileToken = Windows::Storage::AccessCache::StorageApplicationPermissions::FutureAccessList->Add(folder2); } else { int i = 100; } }); } });
我尝试使用 那个帖子中的方法 GetFolderFromPathAsync ,编译后运行报错 “error:传递了一个无效参数”
你能帮我看看错误在哪里吗?
- 已编辑 李磊和韩梅梅2 2013年8月7日 8:58
-
对于文件夹来说Path最后的地方应该没有双斜杠把
James He
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已编辑 Jamles HezModerator 2013年8月7日 9:18 update
- 已标记为答案 李磊和韩梅梅2 2013年8月7日 9:21
-
恭喜啊。
不过上午我也做过类似的试验,我的代码是C#的
首先通过OpenFilePicker选择了文件并且推导出了文件夹的位置:
string[] paths = file.Path.Split('\\'); string folderPath = ""; for (int i = 0; i < paths.Count<string>() - 1; i++) { folderPath += paths[i]; if (i != paths.Count<string>() - 2) { folderPath += "\\"; } }
然后通过给出Token的方式
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folderPath);
var fileToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(folder);
当我选择的是一个在Manifest中注册的文件夹时候可以访问,但是如果是其他文件夹就会报错An exception of type 'System.UnauthorizedAccessException' occurred
所以我之前的帖子才告诉你权限可能就是个问题。
James He
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已编辑 Jamles HezModerator 2013年8月7日 9:50 update