如何关闭打开的StorageFile?

时间:2015-10-05 12:26:49

标签: c# .net windows-runtime windows-phone-8.1 windows-phone

我正在撰写 Windows Phone 8.1(WINRT XAML)应用

在方法1中,我打开StorageFile ,而在方法2中,我正在写同一个文件。但它在写入文件时显示错误。

  1. 打开和阅读内容后如何关闭此文件?
  2. 然后如何在方法2中写入文件后再次关闭此文件?

    private async Task<bool> Method1(int workOutID)
    {
        string filename = "WorkoutOfflineDataFile.txt";
        StorageFolder LocalStorageFolderObject = ApplicationData.Current.LocalFolder;
        StorageFile StorageFileObject = await LocalStorageFolderObject.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
        string JSONFromFile = await FileIO.ReadTextAsync(StorageFileObject);
    }
    
    private async void Method2()
    {
        string filename = "WorkoutOfflineDataFile.txt";
        StorageFolder LocalStorageFolderObject = ApplicationData.Current.LocalFolder;
        StorageFile StorageFileObject = await LocalStorageFolderObject.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
        await FileIO.WriteTextAsync(StorageFileObject, JSonData_ToSave);
    }
    
  3.   

    ERROR:

         

    抛出异常:&#39; System.UnauthorizedAccessException&#39;在   mscorlib.ni.dll

         

    其他信息:访问被拒绝。 (HRESULT的例外情况:   0x80070005(E_ACCESSDENIED))

3 个答案:

答案 0 :(得分:1)

代码看似正确,我认为异常是由别的东西引发的。

无论如何,如果您想确保在读/写操作后关闭文件和流,您可以使用一次性方式:

private async Task<bool> Method1(int workOutID)
{
    string filename = "WorkoutOfflineDataFile.txt";
    StorageFolder LocalStorageFolderObject = ApplicationData.Current.LocalFolder;
    StorageFile StorageFileObject = await LocalStorageFolderObject.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
    using (var stream = await StorageFileObject.OpenStreamForReadAsync())
    {
        using (var reader = new StreamReader(stream))
        {
            string JSONFromFile = reader.ReadToEnd();
        }
    }
}

private async void Method2()
{
    string filename = "WorkoutOfflineDataFile.txt";
    StorageFolder LocalStorageFolderObject = ApplicationData.Current.LocalFolder;
    StorageFile StorageFileObject = await LocalStorageFolderObject.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
    using (var stream = await StorageFileObject.OpenStreamForWriteAsync())
    {
        using (var writer = new StreamWriter(stream))
        {
            writer.Write(JSonData_ToSave);
        }
    }
}

答案 1 :(得分:0)

Festyk的解决方案是否有效?也许只是尝试使用:

StorageFile StorageFileObject = await LocalStorageFolderObject.GetFileAsync(filename);

在你的第二种方法中,如果你确定文件已经存在 - 请记住,否则会引发异常。

答案 2 :(得分:0)

您正在使用method1()

锁定文件
private async Task<bool> Method1(int workOutID)
{
    string filename = "WorkoutOfflineDataFile.txt";
    StorageFolder LocalStorageFolderObject = ApplicationData.Current.LocalFolder;
    StorageFile StorageFileObject = await LocalStorageFolderObject.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
    string JSONFromFile = await FileIO.ReadTextAsync(StorageFileObject);
}

当您尝试访问WorkoutOfflineDataFile.txt时,您实际上是在锁定此文件,因此在关闭之前没有其他线程可以触摸它。

卢卡的回答似乎是好的和安全的。尝试使用它。