Ejemplo n.º 1
0
 private void button6_Click(object sender, EventArgs e)
 {
     if (FileOperation.GetFileSize(FullPath) == 0)
     {
         string[] strArray = GetData();
         File.WriteAllLines(FullPath, strArray);
     }
     //如果里面已经有了记录
     else
     {
         string[] strArray = GetData();
         //下面就是一种插入数据的方式
         File.AppendAllLines(FullPath, strArray);
     }
     button5.Enabled = true;
     button6.Enabled = false;
     MessageBox.Show("上传成功!");
 }
Ejemplo n.º 2
0
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title  = "选择要打开的txt文件";
            ofd.Filter = "AllFiles(*.*)|*.*|TXT(*.*)|*.txt";
            ofd.ShowDialog();//显示选取图像路径的窗口
            textBox1.Visible = true;
            textBox1.Text    = ofd.FileName;
            //ImgPath = ofd.FileName;
            if (!(File.Exists(ofd.FileName)))
            {
                MessageBox.Show("没有选取文件", "提示");
                return;
            }
            //判断是不是txt
            string        fileName    = ofd.FileName;
            FileOperation judgeSample = new FileOperation(fileName);
            isTxtDelegate isTxtSample = new isTxtDelegate(judgeSample.isTXT);

            if (isTxtSample(fileName))
            {
                label2.Visible  = true;
                button2.Enabled = true;
            }
            else
            {
                label3.Visible = true;
            }
            FullPath = textBox1.Text;

            long SizeOfFile = FileOperation.GetFileSize(FullPath);

            if (SizeOfFile > 650)
            {
                MessageBox.Show("本程序目前不支持处理多条记录的文件,多条记录更新无效。");
                button2.Enabled = false;
            }
            else
            {
                Regex regex1 = new Regex(@"SerialNumber:(\w*)");
                Regex regex2 = new Regex(@"Price:(\d*),");
                Regex regex3 = new Regex(@"Builder:(\w*)");
                Regex regex4 = new Regex(@"Model:(\w*)");
                Regex regex5 = new Regex(@"Type:(\w*)");
                Regex regex6 = new Regex(@"BackWood:(\w*)");
                Regex regex7 = new Regex(@"TopWood:(\w*)");

                //1  读取数据
                //string text = System.IO.File.ReadAllText(FullPath);
                //Console.WriteLine(text);
                //2  按行读取,按行输出

                DataStruct DS1   = new DataStruct();
                string[]   lines = System.IO.File.ReadAllLines(FullPath);
                foreach (string line in lines)
                {
                    string contend = "";
                    //这里就是非常适合委托和lambda了。装一波逼。
                    if (regex1.IsMatch(line))
                    {
                        contend          = regex1.Match(line).Groups[1].Value;
                        DS1.SerialNumber = contend;
                        Console.WriteLine(contend);
                        textBox2.Text = contend;
                    }
                    else if (regex2.IsMatch(line))
                    {
                        contend = regex2.Match(line).Groups[1].Value;
                        long mylong = long.Parse(contend);
                        DS1.Price = mylong;
                        Console.WriteLine(contend);
                        numericUpDown1.Value = mylong;
                    }
                    else if (regex3.IsMatch(line))
                    {
                        contend     = regex3.Match(line).Groups[1].Value;
                        DS1.Builder = contend;
                        Console.WriteLine(contend);
                        textBox7.Text = contend;
                    }
                    else if (regex4.IsMatch(line))
                    {
                        contend   = regex4.Match(line).Groups[1].Value;
                        DS1.Model = contend;
                        Console.WriteLine(contend);
                        textBox4.Text = contend;
                    }
                    else if (regex5.IsMatch(line))
                    {
                        contend = regex5.Match(line).Groups[1].Value;
                        Console.WriteLine(contend);
                        DS1.Type      = contend;
                        textBox6.Text = contend;
                    }
                    else if (regex6.IsMatch(line))
                    {
                        contend      = regex6.Match(line).Groups[1].Value;
                        DS1.BackWood = contend;
                        Console.WriteLine(contend);
                        textBox3.Text = contend;
                    }
                    else if (regex7.IsMatch(line))
                    {
                        contend     = regex7.Match(line).Groups[1].Value;
                        DS1.TopWood = contend;
                        Console.WriteLine(contend);
                        textBox5.Text = contend;
                    }
                    button2.Enabled = true;
                }
                //3  按行读取,按行输出
                //using (System.IO.StreamReader sr = new System.IO.StreamReader(FullPath))
                //{
                //    string str;
                //    while ((str = sr.ReadLine()) != null)
                //    {
                //        Console.WriteLine(str);
                //    }
                //}
            }
        }