问题
I'm encountering strange issue when app is deployed from Windows Store (beta version). The app is written as Windows Phone 8.1 RunTime.
I've a small windows runtime component written in C++/C# which checks for file existence:
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;
}
TESTING METHOD:
Now let's test it with two files - one created in local folder and one in folder in MusicLibrary. Everything is done in main project, which has a reference to WRC with above method in C++/C#.
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) Local file Fist - test with pure 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);
Second with written component:
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) The same with file in music library folder:
Fist - test with pure 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);
Second with written component:
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:
There is no exception in any case, as pure C# methods show, both files exists after their creation. As you can see in the pictures attached below, when app is deployed via Visual Studio, it works properly, runtime component shows both files, but when app is download from store, the situation is different - the WRC method works for local files, but not for these in MusicLibrary.
QUESTION:
In both cases file paths are the same, in both deployments runtime component works, hence the first file exists. It seems like windows runtime component doesn't have access to MusicLibrary, despite all necessary capabilities being set in packageappx.manifest file (local deployment works).
Does anyone have an idea why the windows runtime component doesn't have access to a file in MusicLibrary?
Does windows runtime component need some additional capabilities?
Any way to make it work?
回答1:
The behavior from the store is correct: the app doesn't have permissions to the file system outside of its installed and app data folders. Win32 and C runtime functions access the files directly and so need direct access permissions.
The StorageFile class works with the file broker and so gets the advantage of the declared capabilities, etc. The broker reads the file on the apps behalf and streams the file contents through the StorageFile.
Apps must use StorageFile to read or write files outside their app data and installed location
We've flagged the behavior difference between testing and production for investigation.
来源:https://stackoverflow.com/questions/27875608/different-app-behaviour-when-deployed-locally-and-from-store