写一个.txt文件的特定部分

时间:2014-03-31 04:17:13

标签: c# streamwriter

编辑我实际上并没有使用.txt文件作为存储有价值数据的首选方法。 此问题的重点是写入.txt文件的特定部分

我使用.txt文件作为虚拟数据库来测试程序,我遇到了重写一些数据的问题。

例如,.txt文件是这样的:

Ben,welcome1,1
Frank,frankpassword,1
etc...

我使用一种简单的方法来检索用户信息:

public void ReadUserFile(User_Model UModel)
{
    importFile = File.ReadAllText(fileName).Split('\n');
    foreach (string line in importFile)
    {
        data = line.Split(',');
        userName = data[0];
        password = data[1];
        accessLevel = Convert.ToInt32(data[2]);

        if (userName == UModel.UserName && password == UModel.UserPassword)
        {
            UModel.AccessLevel = accessLevel;
            if (UModel.UserPassword == "welcome1")
            {
                UModel.UserPassword = "change password";
                break;
            }
            break;
        }
        else { UModel.UserPassword = "invalid"; }
        lineCount++;
    }
}

然后我开始编写一种重新编写密码的方法,如果密码存储为' welcome1',但是当它出现时,我不确定该怎么做,或者如果它甚至可以完成?

例如

UModel.UserName = "Ben";
UModel.UserPassword = "welcome1";
UModel.ConfirmPassword = "newpassword";

public void UpdateUserFile(User_Model UModel)
{
    importFile = File.ReadAllText(fileName).Split('\n');
    foreach (string line in importFile)
    {
        data = line.Split(',');
        userName = data[0]; // "Ben"
        password = data[1]; // "welcome1"

        if (data[0] == UModel.UserName && UModel.UserPassword == data[1])
        {
            // Re-write "Ben,welcome1,1" to "Ben,newpassword,1"
        }
    }
}

3 个答案:

答案 0 :(得分:1)

如果它是虚拟测试数据库并且性能不是问题,最简单的方法是只读取所有行,根据需要修改它们,然后从头开始重写整个文件。它比以本质上是线性文件格式实现就地编辑要容易得多。

如果你真的想要就地编辑文件,请阅读使用StreamWriter类将文件作为FileStream打开,跳转到所需位置,并为其写一行。这可能需要使用随附的StreamReader进行原始文件读取,以便您知道要替换的行的确切文件位置。

答案 1 :(得分:1)

根据文本文件的大小,至少有两个选项:

  1. 如果文本文件只应该是几行,请使用此解决方案:

    importFile = File.ReadAllText(fileName).Split('\n');
    StringBuilder newContents = new StringBuilder();
    
    foreach (string line in importFile)
    {
        data = line.Split(',');
        userName = data[0]; // "Ben"
        password = data[1]; // "welcome1"
    
        if (data[0] == UModel.UserName && UModel.UserPassword == data[1])
        {
            line = data[0] + "," + UModel.ConfirmPassword + "," + data[2];
    
            newContents.Append(line);
            newContents.AppendLine();
        }
    }
    
    File.WriteAllText(fileName, newContents.ToString());
    
  2. 如果文本文件非常庞大,那么您需要使用2个文件。

  3. 逐行读取一个文件,逐行将其写入另一个临时文件,直到找到匹配的行,此时您将修改的行写入新文件,然后继续写入所有剩余的行 - 来自第一个文件。最后,您需要删除旧文件并将新文件重命名为旧文件。

答案 2 :(得分:0)

这是一个根据您的用户名更新密码的简单程序。

   static void Main(string[] args)
    {


        string[] a = File.ReadAllLines(@"C:/imp.txt");// to read all lines

        Console.WriteLine("enter a name whose password  u want to change");
        string g=Console.ReadLine();
        Console.WriteLine("enter new password");
        string j = Console.ReadLine();
        for(int i=0;i<a.Length;i++)                  
        {

            string[] h = a[i].Split(',');
            if(h[0]==g)
            {
                a[i] = h[0] + ","+j+"," + h[2];
                break;// after finding a specific name,you dont need to search more 
            }

        }

        File.WriteAllLines(@"C:/imp.txt",a);//to write all data from " a "  into file


    }
相关问题