从 Windows 应用商店(测试版)部署应用程序时,我遇到奇怪的问题。该应用程序被编写为 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) 本地文件
拳头 - 使用纯 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);
RESULTS:
任何情况下都不例外,正如纯 C# 方法所示,这两个文件在创建后都存在。正如您在下面的图片中看到的,当通过 Visual Studio 部署应用程序时,它可以正常工作,运行时组件显示这两个文件,但是当从商店下载应用程序时,情况有所不同 - WRC 方法适用于本地文件,但是不适用于 MusicLibrary 中的这些。
问题:
在这两种情况下,文件路径相同,在两种部署中运行时组件都可以工作,因此第一个文件存在。尽管在中设置了所有必要的功能,但 Windows 运行时组件似乎无法访问 MusicLibrarypackageappx.manifest文件(本地部署有效)。
有谁知道为什么 Windows 运行时组件无法访问 MusicLibrary 中的文件?
Windows 运行时组件是否需要一些额外的功能?
有什么办法让它发挥作用吗?