IsolatedStorageFileStream异常不允许操作

本文关键字:操作 不允许 异常 IsolatedStorageFileStream | 更新日期: 2023-09-27 18:04:07

好吧,我已经搜索并尝试了所有可能的解决方案,但仍然抛出异常。如果你能帮忙就太好了。

public void SaveOrReplace(string fileContent)
        {
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!storage.DirectoryExists("FX"))
                    storage.CreateDirectory("FX");
                if (storage.FileExists(FilePath))
                    storage.DeleteFile(FilePath);
                //when I debug, it code does not get past here
                // when I remove this first using no exception, but of course I can't write some text to the file
                using (IsolatedStorageFileStream fileStream = storage.CreateFile(FilePath))
                {
                    using (StreamWriter writer = new StreamWriter(fileStream))
                    {
                        writer.Write(fileContent);
                        fileStream.Dispose();
                    }
                }
            }
        }

堆栈跟踪信息

   at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf)
   at System.IO.IsolatedStorage.IsolatedStorageFile.CreateFile(String path)
   at FXNews.Models.FFNewsProvider.SaveOrReplace(String fileContent)
   at FXNews.Models.FFNewsProvider.<Load>d__b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at FXNews.Models.FFNewsProvider.<GetNewsCollection>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at FXNews.ViewModels.PivotPageViewModel.<GetAllNews>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at FXNews.Views.PivotPage.<OnNavigatedTo>d__2.MoveNext()

在这种情况下我做错了什么?

IsolatedStorageFileStream异常不允许操作

好了,在随机摆弄代码之后,我设法使它正确。老实说,我不确定背后的原因,但它是有效的。

public void SaveOrReplace(string fileContent)
{
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!storage.DirectoryExists("FX"))
            storage.CreateDirectory("FX");
        if (storage.FileExists(FilePath))
            storage.DeleteFile(FilePath);
        using (IsolatedStorageFileStream fileStream = storage.OpenFile(FilePath, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
        {
            using (StreamWriter writer = new StreamWriter(fileStream))
            {
                writer.Write(fileContent);
                writer.Dispose();
            }
            fileStream.Dispose();
        }
    }
}

我改变了FilePath

private const string FilePath = "FX/ffcal.xml"; // does not work, throw a different exception after I did above changes
private const string FilePath = "FX/ffcal.txt"; // this works fine, save the xml in txt and parse it. It works okay