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

C# 基于某个起始字符串读取WP7中文本文件的特定行并替换(覆盖)它,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,我能够在WP7应用程序中对存储在独立存储中的文本文件执行读/写/追加操作 我的场景是,我将空间分隔的值存储在文本文件中的独立存储中 所以,如果我必须为某一行找到一些起始键,那么如何覆盖 该键的值,而不影响其前后的另一行 例如: 关键字值SomeOtherValue *状态良好 状态1未读错误 状态2空cantsay* 因此,如果我必须根据某些条件改变整个第二行,关键点相同 状态1读取良好 如何实现这一点?有许多方法可以实现这一点,您选择的方法应该最适合数据文件的大小和复杂性 开始的一个选项是使用

我能够在WP7应用程序中对存储在独立存储中的文本文件执行读/写/追加操作

我的场景是,我将空间分隔的值存储在文本文件中的独立存储中

所以,如果我必须为某一行找到一些起始键,那么如何覆盖 该键的值,而不影响其前后的另一行

例如:

  • 关键字值SomeOtherValue
  • *状态良好
  • 状态1未读错误
  • 状态2空cantsay*
  • 因此,如果我必须根据某些条件改变整个第二行,关键点相同

    状态1读取良好


    如何实现这一点?

    有许多方法可以实现这一点,您选择的方法应该最适合数据文件的大小和复杂性

    开始的一个选项是使用静态方法。这是粗糙的,但如果您的文件很小,那么它就没有问题

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

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

    开始的一个选项是使用静态方法。这是粗糙的,但如果您的文件很小,那么它就没有问题

    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数据库可能是一个性能更好的解决方案。

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

    // 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数据库可能是一个性能更好的解决方案。

    我已经知道这样做是可能的,但实际上我正在考虑一些简短而有效的方法,因为我的文件有150行,有许多文件需要重写。如果access为ReadWrite,并且在遍历文件行时发现匹配项,我还可以写入/替换这些值。@平移-这些是您问题的一部分的详细信息。。。如果文件有150行长,那么将其存储为XML并使用XPath查询应该会获得更好的性能。如果您有许多这样的文件,那么也许您应该开始考虑在独立存储中使用数据库(SQLCE)——这会更简单。在任何情况下,我已经回答了你提出的问题,也许你需要问一个新问题或修改你现有的问题?当我想起来的时候,你在主持人选举中投票了吗?如果没有,就到那边去做:)谢谢你的回复,也谢谢你的忠告。我将来一定会记住这一点。但我的要求是这样的,我只需要在txt文件中写入数据,以支持其他平台,如ios和android,因为我正在与其他平台共享此txt文件。我找不到一种方法,在读取txt文件时是否可以替换match上的一行。无论如何,谢谢你的帮助。我已经知道这是可能的,但实际上我正在考虑一些简短而有效的方法,因为我的文件有150行,有很多文件我需要重写。如果access为ReadWrite,并且在遍历文件行时发现匹配项,我还可以写入/替换这些值。@平移-这些是您问题的一部分的详细信息。。。如果文件有150行长,那么将其存储为XML并使用XPath查询应该会获得更好的性能。如果您有许多这样的文件,那么也许您应该开始考虑在独立存储中使用数据库(SQLCE)——这会更简单。在任何情况下,我已经回答了你提出的问题,也许你需要问一个新问题或修改你现有的问题?当我想起来的时候,你在主持人选举中投票了吗?如果没有,就到那边去做:)谢谢你的回复,也谢谢你的忠告。我将来一定会记住这一点。但我的要求是这样的,我只需要在txt文件中写入数据,以支持其他平台,如ios和android,因为我正在与其他平台共享此txt文件。我找不到一种方法,在读取txt文件时是否可以替换match上的一行。无论如何,谢谢你的帮助。