Exemplo n.º 1
0
        public void run()
        {
            if (radioButton3.Checked == true)
            {
                keysr = new StreamReader(textBox3.Text, EncodingType.GetTxtType(textBox3.Text));

                ReadTxt = keysr.ReadToEnd();
            }

            string        path   = textBox1.Text;
            DirectoryInfo folder = new DirectoryInfo(path);
            int           c      = folder.GetFiles("*.txt").Count();

            for (int i = 0; i < c; i++)
            {
                try
                {
                    label4.Text = "共" + c + "已处理:" + (i + 1);
                    string fullname = folder.GetFiles("*.txt")[i].FullName;
                    // textBox4.Text += "正在处理:" + fullname + "\r\n";
                    StringBuilder sb = new StringBuilder();

                    String ReadTxt;

                    StreamReader sr = new StreamReader(fullname, EncodingType.GetTxtType(fullname));

                    ReadTxt = sr.ReadToEnd();


                    //首先判断是否有img
                    if (ReadTxt.Contains("img"))
                    {
                        string[] FileRead = ReadTxt.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                        foreach (String ReadLine in FileRead)
                        {
                            string key = chuli(fullname);
                            if (key == "")
                            {
                                key = Path.GetFileNameWithoutExtension(fullname);
                            }
                            string linevalue = ReadLine;
                            if (ReadLine.Contains("img"))
                            {
                                if (checkBox1.Checked == true && !ReadLine.Contains("title"))
                                {
                                    linevalue = linevalue.Replace("web_uri", "title=\"" + key + "\"  web_uri");
                                }
                                if (checkBox2.Checked == true && !ReadLine.Contains("alt"))
                                {
                                    linevalue = linevalue.Replace("web_uri", "alt=\"" + key + "\"  web_uri");
                                }

                                sb.AppendLine(linevalue);
                            }
                            else
                            {
                                sb.AppendLine(ReadLine);
                            }
                        }
                        sr.Close();
                        sr.Dispose();
                        StreamWriter sw = new StreamWriter(fullname);

                        sw.Write(sb.ToString());

                        sw.Close();
                        sw.Dispose();
                    }
                }

                catch (Exception ex)
                {
                    continue;
                }
            }

            MessageBox.Show("处理结束");
            keysr.Close();
            keysr.Dispose();
        }