dat文件中读取数据并将新字符串写入同一文件

dat文件中读取数据并将新字符串写入同一文件

本文介绍了从* .dat文件中读取数据并将新字符串写入同一文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次用户输入新密码时,我都有一个写入dat文件的程序。我希望能够像这样编码密码1 | ****** |文件夹| 2 | **** |不同的文件夹。有没有办法从文件读取到一个字符串,并找出文件中有多少子串并除以3并使用该数字作为下一个密码?这是编写它的代码。

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

推荐答案

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.


这篇关于从* .dat文件中读取数据并将新字符串写入同一文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:49