错误“IsolatedStorageFileStream上不允许操作”。 WP7

时间:2012-03-03 05:17:30

标签: windows-phone-7 windows-phone-7.1 isolatedstorage

我正在尝试从IsolatedStorage读取文本文件,并检查它是否包含字符串。如果不是,则将字符串添加到文件的末尾。但是当我尝试将字符串写入文件时,我收到了一个错误:"Operation not permitted on 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;
   }
}

1 个答案:

答案 0 :(得分:5)

我认为您遇到的问题与通过新建IsolatedStorageFileStream创建StreamWriter的行有关 - 当您已经从downloadFile.CreateFile()调用的返回中获得了正确的行时。

试试这段代码,我认为它可以做你想做的事情:

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();
                }

            }
        }
    }
}