Hellllooooo家伙!
我有一个适用于Windows Phone 8的应用程序。我的应用程序的崩溃报告列出了一个经常发生的非常大的错误(崩溃计数很高)。有趣的(让我们说悲伤)是我无法在手机或模拟器上重现此错误。你能帮帮我吗?
错误是:
problem function: MyAppName.More+_writeToFile_d__11.MoveNext
exception type: system.unauthorizedaccessexception
stacktrace: "Frame Bild Funktion Offset
0 mscorlib_ni
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
0x0023a00e
1 mscorlib_ni
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
0x0000003a
2 mscorlib_ni System.Runtime.CompilerServices.TaskAwaiter_
[[System.__Canon,_mscorlib]].GetResult 0x0000001c
3 myAppName_ni
MyAppName.More+_writeToFile_d__11.MoveNext
0x000001cc
4 mscorlib_ni
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
0x0023a00e
5 mscorlib_ni
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
0x0000003a
6 mscorlib_ni
System.Runtime.CompilerServices.TaskAwaiter.GetResult
0x0000001a
7 myAppName_ni
MyAppName.More+_readFile_d__1b.MoveNext
0x000002f8
8 mscorlib_ni
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess
0x0023a00e
9 mscorlib_ni
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
0x0000003a
10 mscorlib_ni
System.Runtime.CompilerServices.TaskAwaiter.GetResult
0x0000001a
11 myAppName_ni
MyAppName.MainPage+_buttonLike_Click_d__60.MoveNext
0x0000073e
12 mscorlib_ni
System.Runtime.CompilerServices.AsyncMethodBuilderCore._ThrowAsync_b__0 0x00000036"
我正在使用方法“writeToFile”:
await readFile();
await writeToFile((pointsCount + 200).ToString());
await readFile();
方法“writeToFile”就是这个:
public static async Task writeToFile(string text)
{
// Get the text data from the parameter.
byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(text.ToCharArray());
// Get the local folder.
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
// Create a new folder name DataFolder.
var dataFolder = await local.CreateFolderAsync("DataFolder",
CreationCollisionOption.OpenIfExists);
// Create a new file named DataFile.txt.
var file = await dataFolder.CreateFileAsync("DataFile.txt",
CreationCollisionOption.ReplaceExisting);
// Write the data from the parameter.
using (var s = await file.OpenStreamForWriteAsync())
{
s.Write(fileBytes, 0, fileBytes.Length);
}
}
方法“readFile”是这样的:
public static async Task readFile()
{
// Get the local folder.
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
if (local != null)
{
if (Directory.Exists(local.Path + @"\DataFolder"))
{
// Get the DataFolder folder.
var dataFolder = await local.GetFolderAsync("DataFolder");
if (File.Exists(dataFolder.Path + @"\DataFile.txt"))
{
// Get the file.
var file = await dataFolder.OpenStreamForReadAsync("DataFile.txt");
if (file.Length > 0)
{
// Read the data.
using (StreamReader streamReader = new StreamReader(file))
{
pointsCount = Convert.ToInt32(streamReader.ReadToEnd());
}
}
else
{
await writeToFile("0");
await readFile();
}
}
else
{
await writeToFile("0");
await readFile();
}
}
else
{
await writeToFile("0");
await readFile();
}
}
}