Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a program that writes to a dat file every time the user enters a new password.I want to be able to number the passwords like this 1|******|Folder|2|****|different folder.Is there a way to read from the file into a string and find out how many substrings in the file are there and divide by 3 and use that number for the next password? here is the code that writes it.
              private void buttonclick(object sender,EventArgs e)
         {         int counter;
                  counter = TextBoxs.Count + 1;
                TextBox tb = new TextBox();
                Label labl = new Label();
                Label num = new Label();
                tb.Text = InputText1.Text;
                labl.Text = "folder"+counter;
                num.Text = counter.ToString();
                TextBoxs.Add(tb);
                string currentEntry = num.Text + "|" + tb.Text.Replace("|", "~") + "|" 
             + labl.Text + "|";
                File.AppendAllText("SavedData.dat", currentEntry);
                this.Hide();
           }

and the code that reads it to the second form

  if (File.Exists("SavedData.dat"))
            {
                using (StreamReader sr = new StreamReader("SavedData.dat"))
                {
                  
                  
                    Point newloc = new Point(175,30);
                    Point numloc = new Point(25,30);
                    Point labloc = new Point(60,30);
                    
                  
                 
                    string line = "";
                    while ((line = sr.ReadLine())!=null)
                    {
                        string[] lineData = line.Split('|');
                        if (lineData.Length >= 3)
                        {
                            Label labl = new Label();
                            Label num = new Label();
                            TextBox tb = new TextBox();
                            tb.Enabled = false;
                            string currentnumber = lineData[0];
                            string currentTbText = lineData[1];
                            string currentLablText = lineData[2];
                            labl.Size = label2.Size;
                            tb.Size = passwordtextbox.Size;
                            tb.UseSystemPasswordChar = true;
                            num.Size = label3.Size;
                            num.Location = numloc;
                            tb.Location = newloc;
                            labl.Location =labloc;
                            tb.Text = currentTbText;
                            labl.Text = currentLablText;
                            num.Text = currentnumber;
                            tb.ReadOnly = true;
                            TextBoxes.Add(tb);
                            labels.Add(labl);
                            nums.Add(num);
                            this.Controls.Add(num);
                            this.Controls.Add(tb);
                           this.Controls.Add(labl);
                        }

                        if (lineData.Length >= 6)
                        {
                            Label labl = new Label();
                            Label num = new Label();
                            TextBox tb = new TextBox();
                            tb.Enabled = false;
                            string currentnumber = lineData[3];
                            string currentTbText = lineData[4];
                            string currentLablText = lineData[5];
                            newloc.Offset(0,tb.Height +5);
                            numloc.Offset(0,tb.Height + 5);
                            labloc.Offset(0,tb.Height +5);
                            labl.Size = label2.Size;
                            tb.ReadOnly = true;
                            tb.Size = passwordtextbox.Size;
                            num.Size = label3.Size;
                            tb.UseSystemPasswordChar = true;
                            num.Location = numloc;
                            tb.Location = newloc;
                            labl.Location = labloc;
                            tb.Text = currentTbText;
                            labl.Text = currentLablText;
                            num.Text = currentnumber;
                            TextBoxes.Add(tb);
                            labels.Add(labl);
                            nums.Add(num);
                            this.Controls.Add(num);
                            this.Controls.Add(tb);
                            this.Controls.Add(labl);

What I have tried:

here is the code i have tried
<pre>
                Streamreader s = new Streamreader();
                string d = s.ReadToEnd();
                string e ;
                  string line = "";
                    while ((line = s.ReadLine())!=null)
                   {string[] lineData = line.Split('|');
                    e = (lineData.Count/3).ToString();}
           counter = TextBoxs.Count + 1;
                TextBox tb = new TextBox();
                Label labl = new Label();
                Label num = new Label();
                tb.Text = InputText1.Text;
                labl.Text ="Folder"+counter;
                num.Text = e;
                    TextBoxs.Add(tb);
                string currentEntry = num.Text + "|" + tb.Text.Replace("|", "~") + "|" + labl.Text + "|";
                File.AppendAllText("SavedData.dat", currentEntry);
                this.Hide();}
Posted
Updated 31-Jul-18 21:57pm
Comments
[no name] 31-Jul-18 20:11pm    
Why all the silly stuff with the text boxes? Nothing to do with the question.
Codingnow20 1-Aug-18 9:03am    
the text boxes show on the second form and that was the only way i could figure out how to accomplish this
Mike V Baker 31-Jul-18 22:13pm    
When you run this code, what do you expect to see in currentEntry and what actually winds up in currentEntry?
These are passwords? And you're writing them to a text file? No encryption? I suggest a re-think there.
Codingnow20 1-Aug-18 9:02am    
im trying to figure this out before i try learning encryption

1 solution

You read all the data from the file, and then try to read a single line:
C#
Streamreader s = new Streamreader();
string d = s.ReadToEnd();   // read all file content into the string d
string e ;
string line = "";
while ((line = s.ReadLine())!=null) // the file is positioned at EOF so nothing to read.

And the use of meaningless single character names for your variables does not help.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900