在 Windows Phone 8.1 的共享文件夹中保存和打开文档文件

Save and Open document files in Shared folder in Windows Phone 8.1

在我的 Windows Phone 8.1 + WIndows Store 8.1 应用程序中,需要下载文件并保存到某个共享位置。然后需要打开相同的文件,用户可以 select/open 通过合适的 native/installed 应用程序。

示例:我可以下载 .pdf,然后发出命令在 Adob​​e Acrobat/user 提供的应用程序中打开此文件。

下载到 LocalFolder 和 RoamingFolder 工作正常,但打开时出现拒绝访问错误。

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
var file = await local.CreateFileAsync(fileName,, CreationCollisionOption.ReplaceExisting); 
stream.Position = 0;
stream.CopyTo(await file.OpenStreamForWriteAsync());
return file.Path;

稍后,将调用以下函数来打开从上面代码下载的文档

Windows.System.Launcher.LaunchUriAsync(new Uri(fileName));

请告诉我如何将文件保存到任何共享文件夹或允许其他应用程序访问并打开它。提前致谢。

请注意,虽然答案不会有所不同,但我在 Xamarin 中的原生 Windows 应用程序解决方案中使用了它。我在 Android native 中实现了类似的功能,效果很好。

后来我发现访问被拒绝的错误不是由于本地或漫游文件夹,而是因为文件在创建时被代码保持打开。

所以代码被替换为:

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
var file = await local.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); 
stream.Position = 0;
using (Stream fileStream = await file.OpenStreamForWriteAsync())
        {
            stream.CopyTo(fileStream);
        }
return file.Path;

以及从应用程序打开文件,将代码替换为:

StorageFile fileToLaunch = await StorageFile.GetFileFromPathAsync(fileName);
bool done = await Windows.System.Launcher.LaunchFileAsync(fileToLaunch, new Windows.System.LauncherOptions { DisplayApplicationPicker = true });

谢谢。