C#字符串相关操作和文件操作

        //打开单个文件
        private void fileBtOpen_Click(object sender, EventArgs e)
        {


            /*
             *打开格式为html文件
            */
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "选择文件";
            fileDialog.Filter = "html files(*.html)|*.html";
            fileDialog.FilterIndex = 1;
            fileDialog.RestoreDirectory = true;
            /*
           *文件的读写操作 
            */

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                Button btn = (Button)sender;
                btn.Enabled = false;

                //将数据写入一个文件
                string filePathName = "e:\\Errorfile.txt";
                fileName.Text = fileDialog.FileName;


                //读写操作流
                StreamReader sr = new StreamReader(fileDialog.FileName, Encoding.Default);
                StreamWriter sw = new StreamWriter(filePathName, true, Encoding.Default);

                //一行一行读文件
                string strLine = sr.ReadLine();
               
                string strheadValue = "";
                string strbodyValue = "";

                //打印html标题
                string strhtmlPathName = fileName.Text;
                int nhtmlBegin = strhtmlPathName.LastIndexOf("\\");
                int nhtmlEnd = strhtmlPathName.LastIndexOf("l");
                string strhtmlName = strhtmlPathName.Substring(nhtmlBegin + 1, nhtmlEnd - nhtmlBegin - 1);
                sw.WriteLine("文件名:" + strhtmlName + "l");



                bool bhead = false;
                bool bbody = false;
                //读文件
                while (strLine != null)
                {
                    //bool body = false;

                    //打印货号

                    if (strLine.Contains("货号:"))
                    {
                        int nstrGoodsBegin = strLine.IndexOf("货号:");
                        int nstrGoodsEnd = strLine.LastIndexOf("</DIV>");
                        strheadValue = strLine.Substring(nstrGoodsBegin + 4, nstrGoodsEnd - nstrGoodsBegin - 5);
                        //sw.WriteLine("head=" + headName);
                        bhead = true;

                    }


                    //打印value值

                    if (bhead == true)
                    {
                        if (strLine.Contains(":tsc"))
                        {
                            string body;
                            int nbodyFirst;
                            int nbodyLast;

                            {
                                //切割字符
                                string[] strSpilt = strLine.Split(' ');
                                for (int i = 0; i < strSpilt.Length; i++)
                                {
                                    body = strSpilt[i].Trim();
                                    if (body.Contains("value="))
                                    {
                                        //截取字符
                                        nbodyFirst = body.IndexOf("=");
                                        nbodyLast = strSpilt[i].Length;
                                        //截取字符
                                        strbodyValue = body.Substring(nbodyFirst + 1, nbodyLast - nbodyFirst - 1);
                                        if (!string.IsNullOrEmpty(strbodyValue))
                                        {
                                            string Cbody1 = strbodyValue.Substring(0, strbodyValue.LastIndexOf("-"));
                                            string Ustr = Cbody1.ToUpper();

                                            if (strheadValue.Equals(Ustr))
                                            {
                                                sw.WriteLine("head=" + strheadValue);
                                                sw.WriteLine("body=" + strbodyValue);
                                                sw.WriteLine("OK");

                                            }
                                            else
                                            {
                                                sw.WriteLine("head=" + strheadValue);
                                                sw.WriteLine("body=" + strbodyValue);
                                                sw.WriteLine("NG");

                                            }


                                        }

                                    }
                                }
                            }
                            bhead = false;
                            bbody = true;



                        }
                    }


                    //读取下一行
                    strLine = sr.ReadLine();
                }
                //关闭文件流
                sr.Close();
                sw.Close();
            }
            MessageBox.Show("文件检查完毕,检查结果文件存放在e:\\Errorfile.txt");

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值