读取WP7中基于某些起始字符串的文本文件的特定行并替换(覆盖)它

本文关键字:替换 覆盖 文本 WP7 于某些 读取 字符串 文件 | 更新日期: 2023-09-27 18:15:03

我能够对WP7应用程序中存储在隔离存储中的文本文件进行读/写/追加操作。

我的场景是,我在文本文件中存储空间分隔的值在隔离的存储。

如果我要找到有起始键的特定行然后如何重写值,而不影响它前后的另一行。

的例子:

  1. 键值SomeOtherValue
  2. *status read good
  3. status1 unread bad
  4. status2 null cannot *

如果我要根据某些条件改变整个第二行让key相同

status1 read good

我怎样才能做到这一点?

读取WP7中基于某些起始字符串的文本文件的特定行并替换(覆盖)它

有许多方法可以做到这一点,您选择的方法应该最适合数据文件的大小和复杂程度。

可以选择使用静态的string.Replace()方法。这是粗糙的,但如果你的文件很小,那么它没有什么问题。

class Program
{
    static void Main(string[] args)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("*status read good");
        sb.AppendLine("status1 unread bad");
        sb.AppendLine("status2 null cantsay*");
        string input = sb.ToString();
        var startPos = input.IndexOf("status1");
        var endPos = input.IndexOf(Environment.NewLine, startPos);
        var modifiedInput = input.Replace(oneLine.Substring(startPos, endPos - startPos), "status1 read good");

        Console.WriteLine(modifiedInput);
        Console.ReadKey();
    }
}

如果您将这些信息存储在文本文件中,那么就没有办法替换整个文件。下面的代码正是这样做的,甚至可能是你现在正在做的。

// replace a given line in a given text file with a given replacement line
private void ReplaceLine(string fileName, int lineNrToBeReplaced, string newLine)
{
    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
        // the memory writer will hold the read and modified lines
        using (StreamWriter memWriter = new StreamWriter(new MemoryStream()))
        {
            // this is for reading lines from the source file
            using (StreamReader fileReader = new StreamReader(new IsolatedStorageFileStream(fileName, System.IO.FileMode.Open, isf)))
            {
                int lineCount = 0;
                // iterate file and read lines
                while (!fileReader.EndOfStream)
                {
                    string line = fileReader.ReadLine();
                    // check if this is the line which should be replaced; check is done by line 
                    // number but could also be based on content
                    if (lineCount++ != lineNrToBeReplaced)
                    {
                        // just copy line from file
                        memWriter.WriteLine(line);
                    }
                    else
                    {
                        // replace line from file
                        memWriter.WriteLine(newLine);
                    }
                }
            }
            memWriter.Flush();
            memWriter.BaseStream.Position = 0;
            // re-create file and save all lines from memory to this file
            using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, System.IO.FileMode.Create, isf))
            {
                memWriter.BaseStream.CopyTo(fileStream);
            }
        }
    }
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    ReplaceLine("test.txt", 1, "status1 read good");
}

我同意slugster的观点:使用SQLCE数据库可能是一个性能更好的解决方案。