-6

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

user3324218
  • 7
  • 1
  • 2

3 Answers3

1

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);
TC Alper Tokcan
  • 369
  • 1
  • 13
  • This is it , but how to bypass line with |1.. – user3324218 Feb 18 '14 at 16:24
  • @user3324218 You can pass the line with using `if()` like : `foreach(string o in accounts){ if(!o.Contains("|1"){ DoWhatYouWant(); })//this means if line not contains "|n" DoWhatYouWant(); ` – TC Alper Tokcan Feb 18 '14 at 16:28
  • Okey, thanks . I have one more question: I used accounts[5] and i exit programme so i need to change in accounts[5] (|1 to |0) but programme change (|1 to |0) in accounts[1] ... – user3324218 Feb 18 '14 at 16:43
  • If you Do something like `accounts[5].Replace("|1","|0");` `accounts[1]` will not change look again your code maybe you miss or not see a code that changes `accounts[1]`. – TC Alper Tokcan Feb 19 '14 at 06:58
0

Something like that :

    string[] lines = File.ReadAllLines(@"C:\filepath.txt");
    List<string> newlines = new List<string>();
    foreach(string line in lines)
    {
      string[] temp = line.Split('|');
      newlines.Add(temp[0] + "|" + temp[1] + "|" + "1");
    }

 File.WriteAllLines(@"C:\filepath.txt", newlines.ToArray())
Habib Rahmoun
  • 218
  • 2
  • 8
0

Look at System.IO.File class, as well as general System.IO.Stream and System.IO.StreamReader classes.

There are a bunch of examples on the internet on how to read and write text files in .NET: http://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.110).aspx

LB2
  • 4,802
  • 19
  • 35