在本地和从Store部署时的不同应用程序行为

当从Windowsapp store(测试版)部署应用时,我遇到了一个奇怪的问题。 该应用程序编写为Windows Phone 8.1 RunTime。

我有一个用C ++ / C#编写的小型Windows运行时组件,用于检查文件是否存在:

bool FileEx::FileExists(String^ path) { std::wstring pathW(path->Begin()); std::string myPath(pathW.begin(), pathW.end()); FILE *file = NULL; if (fopen_s(&file, myPath.c_str(), "r") == 0) { fclose(file); return true; } else return false; } 

测试方法:

现在让我们用两个文件测试它 – 一个在本地文件夹中创建,一个在MusicLibrary中的文件夹中创建。 一切都在主项目中完成,在C ++ / C#中使用上述方法引用WRC。

 const string localFileName = "local.txt"; const string musicFileName = "music.txt"; StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFolder musicFolder = await KnownFolders.MusicLibrary.CreateFolderAsync("MyFolder", CreationCollisionOption.OpenIfExists); await localFolder.CreateFileAsync(localFileName, CreationCollisionOption.ReplaceExisting); // create local file await musicFolder.CreateFileAsync(musicFileName, CreationCollisionOption.ReplaceExisting); // create file in MusicLibrary 

a)本地文件Fist – 使用纯C#测试:

 // First check with C# if file exists - LOCAL FILE StorageFile checkFile = null; try { checkFile = await localFolder.GetFileAsync(localFileName); } catch { checkFile = null; } if (checkFile != null) await Trace.WriteLineAsync(false, "File exists with path = {0}", checkFile.Path); else await Trace.WriteLineAsync(false, "File doesn't exist with path = {0}", checkFile.Path); 

第二个是书面组件:

 Exception exc = null; bool check = false; try { string path = string.Format(@"{0}\{1}", localFolder.Path, localFileName); await Trace.WriteLineAsync(false, "Attempt with WRC path = {0}", path); check = FileEx.FileExists(path); } catch (Exception ex) { exc = ex; } if (exc != null) await Trace.WriteLineAsync(false, "Exception WRC"); else await Trace.WriteLineAsync(false, "No exception WRC, file exists = {0}", check); 

b)与音乐库文件夹中的文件相同:

拳头 – 用纯C#测试:

 checkFile = null; try { checkFile = await musicFolder.GetFileAsync(musicFileName); } catch { checkFile = null; } if (checkFile != null) await Trace.WriteLineAsync(false, "File exists with path = {0}", checkFile.Path); else await Trace.WriteLineAsync(false, "File doesn't exist with path = {0}", checkFile.Path); 

第二个是书面组件:

 check = false; exc = null; try { string path = string.Format(@"{0}\{1}", musicFolder.Path, musicFileName); await Trace.WriteLineAsync(false, "Attempt with WRC path = {0}", path); check = FileEx.FileExists(path); } catch (Exception ex) { exc = ex; } if (exc != null) await Trace.WriteLineAsync(false, "Exception WRC"); else await Trace.WriteLineAsync(false, "No exception WRC, file exists = {0}", check); 

结果:

在任何情况下都没有例外,正如纯C#方法所示,两个文件在创建后都存在。 正如您在下面的图片中看到的,当通过Visual Studio部署应用程序时,它运行正常,运行时组件显示两个文件,但是当从商店下载应用程序时,情况不同 – WRC方法适用于本地文件,但是不适用于MusicLibrary中的这些内容。

本地部署

商店部署

题:

在这两种情况下,文件路径都是相同的,在两个部署中,运行时组件都可以工作,因此存在第一个文件。 尽管在packageappx.manifest文件中设置了所有必要的function(本地部署工作),但似乎Windows运行时组件无法访问MusicLibrary。

有谁知道为什么Windows运行时组件无法访问MusicLibrary中的文件? Windows运行时组件是否需要一些额外的function?
有什么方法可以使它工作?

来自商店的行为是正确的:应用程序没有对其已安装和应用程序数据文件夹之外的文件系统的权限。 Win32和C运行时函数直接访问文件,因此需要直接访问权限。

StorageFile类与文件代理一起使用,因此可以获得声明的function等优势。代理代表应用程序读取文件,并通过StorageFile流式传输文件内容。

应用必须使用StorageFile读取或写入其应用数据和安装位置之外的文件

我们已经标记了测试和生产之间的行为差​​异以进行调查。