Search code examples
c#filetext-editoraccounts

C# How to modify text file


I need to read accounts.txt and add/change number after password

Here is accounts.txt

user|password
user1|password1

After starting

user|password|1
user1|password1|1

After closing

user|password|0
user1|password1|0

Sorry for my english


Solution

  • Add this reference first System.IO Then:

    For reading:

    string[] accounts= File.ReadAllLines("accounts.txt");
    //each item of array will be your account
    

    For modifying :

    accounts[0] += "|1";//this adds "|1" near password
    accounts[0].Replace("|1","|0"); this will change the "|0" text to "|1"
    

    And For writing:

    File.WriteAllLines("accounts.txt",accounts);