C#抓取文件中的某些数据

我是在抓取.log文件中一个C30 之后的第一个+号到{之间的数据

--点击事件

private void button1_Click(object sender, EventArgs e)

        {




--文件路径是个listbox

--文件名是个listbox


            
            string strFullPath = Application.StartupPath;

            this.文件路径.Items.Add(strFullPath);

          --设置搜索路径为当前路径

            for (int i = 0; i < 文件路径.Items.Count; i++)
            {




                DirectoryInfo TheFolder = new DirectoryInfo(文件路径.Items[i].ToString());


                try
                {
                    foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
                    {
                        try
                        {
                            this.文件路径.Items.Add(NextFolder.FullName);


                        }
                        catch (Exception)
                        {




                        }


                    }
                }
                catch (Exception)
                {




                }
                //遍历文件夹


                //遍历文件
                try
                {
                    foreach (FileInfo NextFile in TheFolder.GetFiles())
                    {
                        try

                        {

--查找文件中包含.log的文件

                              if (NextFile.Name.ToString().Contains(".log"))
                            {
                                

                                System.IO.FileInfo file = new System.IO.FileInfo(NextFile.FullName);

                         --获取文件名

                                if (file.Exists)
                                {
                                    string name = file.Name;
                                    name = name.Substring(0, name.IndexOf("_")).ToString();
                                    --我这个是去除了文件名_之后的内容。
                                    

                            string a = File.ReadAllText(file.ToString());

                             --读取文件内容在A中

                              a = a.Remove(0, a.IndexOf("c30"));

                              --先去除了文件中C30索引之前的内容

                             a= a.Remove(0, a.IndexOf("+"));

                             --去除了+号之前的内容

                              --锁定了第一C30之后第一个加号的内容

                             string c30=( a.Substring(1, a.IndexOf("{")-1).ToString())

                             --抓取第一个(0开始)索引到{字符索引的内容;

                             listBox3.Items.Add(c30);
                                    //while ((content = sR.ReadLine()) != null)
                                    //{
                                    //    listBox2.Items.Add(content.ToString());
                                    //    string pattern = @"$'C30";
                                        
                                    //}

                            int z;

                        把他插到数据库,方便整理

                            z = SqlDesigner.ExecuteNoQuery("insert into ICTlogC30 values('" + name + "','" + c30 + "') ");
                                }
                              }
                        }
                        catch (Exception)
                        {




                        }


                    }
                }
                catch (Exception)
                {




                }


            }
            
           


          
            


        }
发布了17 篇原创文章 · 获赞 4 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/ruo40018293/article/details/80234637
今日推荐