从Windows应用商店(测试版)部署应用程序时,我遇到一个奇怪的问题。该应用程序被编写为Windows Phone 8.1运行时。
我有一个用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)本地文件拳头-使用纯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文件中设置了所有必要的功能,但Windows运行时组件似乎无法访问MusicLibrary(本地部署有效)。
有谁知道为什么Windows运行时组件无法访问MusicLibrary中的文件?Windows运行时组件是否需要一些其他功能?
有什么办法可以使其工作?
存储区中的行为是正确的:该应用程序在其已安装和应用程序数据文件夹之外没有对该文件系统的权限。Win32和C运行时函数直接访问文件,因此需要直接访问权限。
StorageFile类与文件代理一起使用,因此可以利用声明的功能等优点。代理代表应用程序读取文件,并通过StorageFile流传输文件内容。
应用程序必须使用StorageFile来读取或写入其应用程序数据和安装位置之外的文件
我们已标记出测试和生产之间的行为差异以进行调查。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句