Windows phone 7 “错误”;不允许对隔离的存储文件流进行操作。”;wp7

Windows phone 7 “错误”;不允许对隔离的存储文件流进行操作。”;wp7,windows-phone-7,windows-phone-7.1,isolatedstorage,Windows Phone 7,Windows Phone 7.1,Isolatedstorage,我正在尝试从IsolatedStorage读取一个文本文件,并检查它是否包含字符串。如果没有,字符串将添加到文件的末尾。但当我试图将字符串写入文件时,我遇到了一个错误:“不允许对IsolatedStorageFileStream执行操作。”。我的代码如下所示。我怎样才能克服这个问题 public void AddToDownloadList() { IsolatedStorageFile downloadFile=IsolatedStorageFile.GetUserStoreForAp

我正在尝试从
IsolatedStorage
读取一个文本文件,并检查它是否包含字符串。如果没有,字符串将添加到文件的末尾。但当我试图将字符串写入文件时,我遇到了一个错误:
“不允许对IsolatedStorageFileStream执行操作。”
。我的代码如下所示。我怎样才能克服这个问题

public void AddToDownloadList()
{
    IsolatedStorageFile downloadFile=IsolatedStorageFile.GetUserStoreForApplication();

    try
    {
        string downloads = string.Empty;

        if (!downloadFile.DirectoryExists("DownloadedFiles"))
            downloadFile.CreateDirectory( "DownloadedFiles" );

        if(downloadFile.FileExists("DownloadedFiles\\DownloadList.txt"))
        {
            IsolatedStorageFileStream downloadStream = downloadFile.OpenFile("DownloadedFiles\\DownloadList.txt",FileMode.Open, FileAccess.Read );

            using ( StreamReader reader = new StreamReader( downloadStream ) )
            {
                downloads = reader.ReadToEnd();
                reader.Close();
            }
            downloadFile.DeleteFile( "DownloadedFiles\\DownloadList.txt" );
        }

        downloadFile.CreateFile( "DownloadedFiles\\DownloadList.txt" );

        string currentFile = FileName;

        if ( !downloads.Contains( currentFile ) )
        {
            downloads += currentFile;

            using ( StreamWriter writeFile = new StreamWriter( new IsolatedStorageFileStream( "DownloadedFiles\\DownloadList.txt", FileMode.Create, FileAccess.Write, downloadFile ) ) )
            {
                writeFile.Write( currentFile + "," );
                writeFile.Close();
            }
        }
   }

   catch ( Exception ex )
   {
       string message = ex.Message;
   }
}

我认为您遇到的问题与通过更新IsolatedStorageFileStream来创建StreamWriter的那一行有关,而此时您应该已经从downloadFile.CreateFile()调用的返回中获得了正确的StreamWriter

试试这段代码,我想它能做你想做的:

public static void AddToDownloadList()
{
    try
    {
        AddToDownloadList("DownloadedFiles", "this file name", "DownloadedFiles\\DownloadList.txt");
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
    }
}

public static void AddToDownloadList(string directory, string fileName, string filePath)
{
    string downloads = string.Empty;
    using (IsolatedStorageFile downloadFile = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!downloadFile.DirectoryExists(directory))
            downloadFile.CreateDirectory(directory);


    if (downloadFile.FileExists(filePath))
        {
            IsolatedStorageFileStream downloadStream = downloadFile.OpenFile(filePath, FileMode.Open, FileAccess.Read);
            using (StreamReader reader = new StreamReader(downloadStream))
            {
                downloads = reader.ReadToEnd();
                reader.Close();
            }
        }

        string currentFile = fileName;
        if (!downloads.Contains(currentFile))
        {
            downloadFile.DeleteFile(filePath);
            using (IsolatedStorageFileStream stream = downloadFile.CreateFile(filePath))
            {

                downloads += currentFile;
                using (StreamWriter writeFile = new StreamWriter(stream))
                {
                    writeFile.Write(currentFile + ",");
                    writeFile.Close();
                }

            }
        }
    }
}