gpt4 book ai didi

windows-phone-7 - 错误 "Operation not permitted on IsolatedStorageFileStream."wp7

转载 作者:行者123 更新时间:2023-12-03 01:02:32 25 4
gpt4 key购买 nike

我正在尝试从 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();
}

}
}
}
}

关于windows-phone-7 - 错误 "Operation not permitted on IsolatedStorageFileStream."wp7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9543704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com