Windows phone 7 windows phone 7正在更新独立存储
在windows phone 7中,更新独立存储文本文件的协议是什么?假设我在一个文本文件中有10个单词,每行1个。现在假设用户使用应用程序,需要在第五行存储一个新词。如何写入文件,该文件已包含10个单词,每行1个单词Windows phone 7 windows phone 7正在更新独立存储,windows-phone-7,insert,isolatedstorage,text-files,Windows Phone 7,Insert,Isolatedstorage,Text Files,在windows phone 7中,更新独立存储文本文件的协议是什么?假设我在一个文本文件中有10个单词,每行1个。现在假设用户使用应用程序,需要在第五行存储一个新词。如何写入文件,该文件已包含10个单词,每行1个单词 提前感谢你们真是太棒了。我一直以来的做法是: 将文件从IsolatedStorage读入menmory 更新字符串 将文件写回存储器 读入文件 写入文件 因此,我会: string fileName = "Test.txt"; string testFile = Isolated
提前感谢你们真是太棒了。我一直以来的做法是: 将文件从IsolatedStorage读入menmory 更新字符串 将文件写回存储器 读入文件 写入文件 因此,我会:
string fileName = "Test.txt";
string testFile = IsolatedStorage_Utility.ReadFromStorage(fileName);
testFile = testFile.Replace("a", "b");
IsolatedStorage_Utility.WriteToStorage(fileName, testFile);
在独立存储中写入文件基本上是一种文件写入操作。这类似于在普通操作系统中访问普通文件并对其进行读写。在您的场景中,如果您确定需要更新10行中的第5行,您将使用流读取器逐行读取,并使用流写入器更新要更新的特定行。您不需要一次又一次地重写所有内容
另一方面,若你们只想添加新的内容,你们可以把它附加到文件的末尾。您可能会发现此链接很有用,因此基本上每次都要覆盖该文件?这样更简单。在几乎所有的操作系统上都是这样做的。想想窗户;你读入一个文件,修改它,然后重写整个过程。从技术上讲,如果您知道要在advanced中更换什么,您只能修改您需要的部件。我还假设您的文件相对较小,并且不会节省很多时间。
public static void WriteToStorage(string filename, string text)
{
try
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
string directory = Path.GetDirectoryName(filename);
if (!storage.DirectoryExists(directory))
storage.CreateDirectory(directory);
if (storage.FileExists(filename))
{
MessageBoxResult result = MessageBox.Show(filename + " Exists\nOverwrite Existing File?", "Question", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.Cancel)
return;
}
using (StreamWriter sw = new StreamWriter(storage.CreateFile(filename)))
{
sw.Write(text);
}
}
}
catch
{
}
}
string fileName = "Test.txt";
string testFile = IsolatedStorage_Utility.ReadFromStorage(fileName);
testFile = testFile.Replace("a", "b");
IsolatedStorage_Utility.WriteToStorage(fileName, testFile);