C#技术百科
问问你的心你有没有信心 做事情要专一坚定,执着

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace Tools
{
    public partial class FormHome : Form
    {
        protected int num = 0;
        protected int i = 0;
        private string filepath = "";
        private string model = "";
        protected string data = "abcdefghi";
        public FormHome()
        {
            InitializeComponent();
            this.noAuto.Visible = true;
        }
        public DataTable GetTable(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = "initial catalog=" + this.tbdata.Text.TrimEnd().ToString() + ";data source=.\\sqlserver2005;user id=" + this.tbuser.Text.TrimEnd() + ";password=" + this.tbpass.Text.TrimEnd();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;

                da.Fill(ds);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            return ds.Tables[0];
        }

        private void BtChange_Click(object sender, EventArgs e)
        {
            StringBuilder tempChangeData = new StringBuilder();
            string tempBeforeData = this.RtbBefore.Text;
            string[] tempLines = tempBeforeData.Split('\n');
            string[] tempSingleLine = null;
            foreach (string temp in tempLines)
            {            
                    tempSingleLine = temp.Split('=');
                     if (tempSingleLine.Length==2)
                    tempChangeData.AppendFormat("{0}={1};\n", tempSingleLine[1].Replace(";", ""), tempSingleLine[0].TrimStart().TrimEnd());            
              
            }
            this.RtbAfter.Text = tempChangeData.ToString();
        }

        private void BtClear_Click(object sender, EventArgs e)
        {
            foreach (Control control in this.TpChange.Controls)
            {
                if (control.GetType().ToString() == "System.Windows.Forms.RichTextBox")
                {
                    ((System.Windows.Forms.RichTextBox)control).Text = "";
                }
            }
        }

        private void BtDataCodeMake_Click(object sender, EventArgs e)
        {
            DataTable dt = this.GetTable("select * from sysobjects where xtype in('u','v') order by status desc");
            for (int i = 0; i < dt.Rows.Count - 1; i++)
            {
                string tableBean = Convert.ToString(dt.Rows[i]["name"] ?? "").ToUpper();
                tableBean = tableBean.Length > 1 ? (tableBean.Substring(0, 1).ToUpper() + tableBean.Substring(1).ToLower()) : (tableBean.ToUpper());
                DataTable dtt = this.GetTable("select * from [" + Convert.ToString(dt.Rows[i]["name"]) + "]");
                System.Text.StringBuilder sbshu = new StringBuilder("using System;").Append(System.Environment.NewLine).Append("using System.Collections.Generic;").Append(System.Environment.NewLine).Append("using System.Text;").Append(System.Environment.NewLine).Append("using System.Data;").Append(Environment.NewLine).Append("using System.Data.SqlClient;").Append(System.Environment.NewLine).Append("").Append(System.Environment.NewLine).Append("namespace ").Append(this.tbNamespace.Text.Trim()).Append(System.Environment.NewLine).Append("{").Append(System.Environment.NewLine);

                System.Text.StringBuilder sbset = new StringBuilder("");

                System.Text.StringBuilder sbbean = new StringBuilder("public " + tableBean + " SelectById(){" + System.Environment.NewLine + tableBean + "  " + tableBean.ToLower().ToString() + " = new " + tableBean + "();" + System.Environment.NewLine + "");
                sbbean.Append("string sql = \"select * from " + tableBean + " where " + dtt.Columns[0].ColumnName.ToString() + "=@" + dtt.Columns[0].ColumnName.ToString() + "\";").Append(Environment.NewLine);
                sbbean.Append("SqlParameter[] pas = {new SqlParameter(\"").Append("@").Append(dtt.Columns[0].ColumnName.ToString()).Append("\"").Append(",").Append(dtt.Columns[0].ColumnName.ToString()).Append(")};").Append(Environment.NewLine);
                sbbean.Append("DataTable dt = db.GetTable(sql,pas);").Append(Environment.NewLine);
                sbbean.Append("  if(dt.Rows.Count>0)").Append(Environment.NewLine).Append("    {").Append(Environment.NewLine);
                //sql前半部分

                StringBuilder sbpas = new StringBuilder("SqlParameter[] pas = {");
                StringBuilder sbadd = new StringBuilder("public bool Add()");
                sbadd.Append("{");
                sbadd.Append(Environment.NewLine);
                sbshu.Append(Environment.NewLine);
                sbshu.Append("public class ");
                sbshu.Append(tableBean);
                sbshu.Append("{");
                sbshu.Append(Environment.NewLine);

                System.Text.StringBuilder sbsqlbefore = new StringBuilder("insert into ").Append(tableBean).Append("(");
                System.Text.StringBuilder sbsqlafter = new StringBuilder(" values(");
                StringBuilder sbupd = new StringBuilder("public bool Update()" + Environment.NewLine + "{" + Environment.NewLine + "string sql =\"update ").Append(tableBean).Append(" set ");
                StringBuilder sbdel = new StringBuilder("public bool Delete()").Append(Environment.NewLine).Append("{").Append("string sql = \"delete ").Append(tableBean).Append(" where ").Append("[").Append(dtt.Columns[0].ColumnName.ToString()).Append("]").Append("=").Append("@").Append(dtt.Columns[0].ColumnName.ToString()).Append("\";").Append(Environment.NewLine);
                StringBuilder sbdelpas = new StringBuilder("SqlParameter[] pas = {");
                StringBuilder sbsel = new StringBuilder("public DataTable Select()").Append(Environment.NewLine).Append("{").Append(Environment.NewLine).Append("DataTable dt = db.GetTable(\"select * from ").Append(tableBean).Append("\")??new DataTable();").Append(Environment.NewLine).Append("return dt;").Append(Environment.NewLine).Append("}");
                //循环字段
                for (int j = 0; j < dtt.Columns.Count; j++)
                {
                    sbshu.Append("private string _" + dtt.Columns[j].ColumnName.ToString() + ";" + System.Environment.NewLine);
                    sbset.Append("public string " + dtt.Columns[j].ColumnName.ToString() + "{set{this._" + dtt.Columns[j].ColumnName.ToString() + "=value;}get{return this._" + dtt.Columns[j].ColumnName.ToString() + ";}}" + System.Environment.NewLine);
                    sbbean.Append("      ").Append(tableBean.ToLower().ToString() + "." + dtt.Columns[j].ColumnName.ToString() + "=Convert.ToString(dt.Rows[0][\"" + dtt.Columns[j].ColumnName.ToString() + "\"]);").Append(System.Environment.NewLine);
                    if (j != 0)
                    {
                        sbsqlbefore.Append(",");
                        sbsqlafter.Append(",");
                    }
                    ///添加
                    sbsqlbefore.Append("[").Append(dtt.Columns[j].ColumnName.ToString()).Append("]");
                    sbsqlafter.Append("@" + dtt.Columns[j].ColumnName.ToString());
                    sbpas.Append("new SqlParameter(\"");
                    sbpas.Append("@");
                    sbpas.Append(dtt.Columns[j].ColumnName.ToString());
                    sbpas.Append("\",");
                    sbpas.Append(dtt.Columns[j].ColumnName.ToString());
                    sbpas.Append(")");
                    if (j < dtt.Columns.Count - 1)
                    {
                        sbpas.Append(",");
                    }
                    ///修改
                    if (j != 0)
                    {
                        sbupd.Append("[").Append(dtt.Columns[j].ColumnName.ToString()).Append("]").Append("=");
                        sbupd.Append("@");
                        sbupd.Append(dtt.Columns[j].ColumnName.ToString());
                        if (j < dtt.Columns.Count - 1)
                        {
                            sbupd.Append(",");
                        }
                    }


                }
                sbpas.Append("};");
                sbsqlbefore.Append(")");
                sbsqlafter.Append(")");
                ///循环字段
                sbadd.Append("string sql = \"").Append(sbsqlbefore.ToString()).Append(sbsqlafter.ToString()).Append("\"").Append(";").Append(Environment.NewLine);
                sbadd.Append(sbpas.ToString()).Append(Environment.NewLine);
                sbadd.Append("return db.GetState(sql,pas);").Append(Environment.NewLine);
                sbadd.Append("}").Append(Environment.NewLine);
                //添加结束
                ///修改
                sbupd.Append(" where ");
                sbupd.Append(dtt.Columns[0].ColumnName.ToString());
                sbupd.Append("=");
                sbupd.Append("@");
                sbupd.Append(dtt.Columns[0].ColumnName.ToString()).Append("\";").Append(Environment.NewLine);
                sbupd.Append(sbpas).Append(Environment.NewLine);
                sbupd.Append("return db.GetState(sql,pas);").Append(Environment.NewLine);
                sbupd.Append("}").Append(Environment.NewLine);
                ///删除
                sbdelpas.Append("new SqlParameter(\"").Append("@").Append(dtt.Columns[0].ColumnName.ToString()).Append("\"").Append(",").Append(dtt.Columns[0].ColumnName.ToString()).Append(")};");
                sbdel.Append(sbdelpas.ToString()).Append(Environment.NewLine);
                sbdel.Append("return db.GetState(sql,pas);").Append(Environment.NewLine).Append("}");
                //查询


                sbshu.Append(sbset.ToString()).Append(System.Environment.NewLine).Append("DbAccess db = new DbAccess();").Append(Environment.NewLine).Append(sbadd.ToString()).Append(Environment.NewLine).Append(sbupd.ToString()).Append(Environment.NewLine).Append(sbdel.ToString()).Append(Environment.NewLine).Append(sbsel.ToString()).Append(Environment.NewLine).Append("//查询").Append(System.Environment.NewLine).Append(sbbean.Append(System.Environment.NewLine).Append("    }").Append(Environment.NewLine).Append("return " + tableBean.ToLower().ToString() + ";" + System.Environment.NewLine + "}").ToString());
                ///
                sbshu.Append(System.Environment.NewLine);

                sbshu.Append(System.Environment.NewLine).Append("}").Append(System.Environment.NewLine).Append("}");
                this.RtbCodes.Text = sbshu.ToString();
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(this.filepath + "\\" + tableBean + ".cs", false, System.Text.Encoding.GetEncoding("gb2312")))
                {
                    sw.Write(sbshu.ToString());
                    sw.Flush();
                }
                this.RtbCodes.Text = sbshu.ToString();

            }
        }

        private void BtBrower_Click(object sender, EventArgs e)
        {
            this.FbdFileBrower.ShowDialog();
            this.filepath = this.FbdFileBrower.SelectedPath.ToString();
            this.model = this.filepath.Substring(this.filepath.LastIndexOf("\\") + 1);
        }

        private void BtResultChange_Click(object sender, EventArgs e)
        {
            ColumnUpdate("<asp:TextBox ID=\"Tb{0}\" runat=\"server\" Width=\"298px\"></asp:TextBox>\n");
        }

        private void BtUpdateColumn_Click(object sender, EventArgs e)
        {
            ColumnUpdate("Tb{0}.Text = "+this.TbTempName.Text+".{0};\n");
        }
        public void ColumnUpdate(string model)
        {
            StringBuilder tempChangeData = new StringBuilder();
            string tempBeforeData = this.RtbColumns.Text;
            string[] tempLines = tempBeforeData.Split(',');
            string[] tempSingleLine = null;
            foreach (string temp in tempLines)
            {
                tempChangeData.AppendFormat(model, temp.Replace("[", "").Replace("]", ""));


            }
            this.RtbColumnsResult.Text = tempChangeData.ToString();
        }

        private void BtOpenFiles_Click(object sender, EventArgs e)
        {
            DirsOpen(this.FbdFilesHtml.SelectedPath);
        }
        public void DirsOpen(string Path)
        {
         
            System.IO.DirectoryInfo dirs = new System.IO.DirectoryInfo(Path);
            foreach (FileInfo fi in dirs.GetFiles("*.htm"))
            {

                Process.Start("IExplore.exe", fi.FullName);
            }
          
            if (dirs.GetDirectories().Length > 0)
            {
                foreach (DirectoryInfo di in dirs.GetDirectories())
                {
                    Dirs(di.FullName);
                }
            }
        }

        private void BtOpen_Click(object sender, EventArgs e)
        {
            this.FbdFilesHtml.ShowDialog();
        }

        private void BtChangeHtml_Click(object sender, EventArgs e)
        {

        }

        private void BtHtmlJs_Click(object sender, EventArgs e)
        {
            StringBuilder tempChangeData = new StringBuilder();
            string tempBeforeData = this.RtbColumns.Text;
            string[] tempLines = tempBeforeData.Split('\n');
            string[] tempSingleLine = null;
            foreach (string temp in tempLines)
            {
                tempChangeData.Append(temp.Replace("\"","'"));
              }
            this.RtbColumnsResult.Text = "document.Write(\""+tempChangeData.ToString()+"\");";
        }

        private void BtSelectFile_Click(object sender, EventArgs e)
        {
            this.OpSelectFile.ShowDialog();
        }

        private void BtOp_Click(object sender, EventArgs e)
        {
            string path = this.OpSelectFile.FileName;
            string content = this.ReadData(path,System.Text.Encoding.GetEncoding("GB2312"));
            this.RtbOpmizationCode.Text = System.Text.RegularExpressions.Regex.Replace(System.Text.RegularExpressions.Regex.Replace(System.Text.RegularExpressions.Regex.Replace(System.Text.RegularExpressions.Regex.Replace(content, ">[\r\t\n ]*", ">"),"[\r\n\t ]*<","<",System.Text.RegularExpressions.RegexOptions.Multiline),">[ ]*<","><"),"[\r\n\t]*","");

           

        }
        /// <summary>
        /// 写文件数据
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        /// <param name="encoding"></param>
        /// <param name="append"></param>
        /// <returns></returns>
        public bool WriteData(string path, string content, System.Text.Encoding encoding,bool append)
        {
            bool state = false;
            try
            {
                using (System.IO.StreamWriter sw = new StreamWriter(path, append, encoding))
                {
                sw.Write(content);
                sw.Flush();
                }
            state = true;
            }
            catch
            {
               
         
            }
            return state;
          
        }
        /// <summary>
        /// 读数据
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string ReadData(string path,System.Text.Encoding encoding)
        {
            string content = "";
            using (System.IO.StreamReader sr = new StreamReader(path, encoding))
            {
                content = sr.ReadToEnd();
            }
            return content;
        }

        private void BtSave_Click(object sender, EventArgs e)
        {
            string path = this.OpSelectFile.FileName;
            this.WriteData(path,this.RtbOpmizationCode.Text,System.Text.Encoding.GetEncoding("Gb2312"),false);
        }

        private void BtTimer_Click(object sender, EventArgs e)
        {
            TimeStart.Interval = 1200;
            if (this.TimeStart.Enabled)
            {
                this.TimeStart.Stop();
            }
            else
            {
                this.TimeStart.Start();
            }
        }

        private void TimeStart_Tick(object sender, EventArgs e)
        {
            string path = this.FbdFileBrower.SelectedPath;
            num++;
            this.LabNum.Text = num.ToString();
            Application.DoEvents();
            //首页
            string tempGroupData = GetHttpData("http://www.domain.com/index.aspx");
            using (StreamWriter sw = new StreamWriter(path + "/Index.html", false, System.Text.Encoding.GetEncoding("utf-8")))
            {
                sw.Write(tempGroupData);
                sw.Flush();
            }
            //产业群首页
            tempGroupData = GetHttpData("http://www.domain.com/Group/index.aspx");
            if (!Directory.Exists(path + "\\Group"))
            {
                Directory.CreateDirectory(path + "\\Group");
            }
            using (StreamWriter sw = new StreamWriter(path + "/Group\\Index.html", false, System.Text.Encoding.GetEncoding("utf-8")))
            {
                sw.Write(tempGroupData);
                sw.Flush();
            }
            TimeStart.Interval = 24 * 60 * 60*1000;
        }
        public string GetHttpData(string sUrl)
        {
            string sRslt = null;
            WebResponse oWebRps = null;
            WebRequest oWebRqst = WebRequest.Create(sUrl);
            oWebRqst.Timeout = 50000;
            try
            {
                oWebRps = oWebRqst.GetResponse();
            }

            finally
            {
                if (oWebRps != null)
                {
                    StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
                    sRslt = oStreamRd.ReadToEnd();
                    oStreamRd.Close();
                    oWebRps.Close();
                }
            }
            return sRslt;
        }

        private void BtWebSite_Click(object sender, EventArgs e)
        {
            this.FbdFileBrower.ShowDialog();
        }

        private void FormHome_Load(object sender, EventArgs e)
        {

        }

        private void FormHome_ResizeBegin(object sender, EventArgs e)
        {
         //   MessageBox.Show(this.WindowState.ToString());
        }

        private void FormHome_ResizeEnd(object sender, EventArgs e)
        {
            //if (this.WindowState == FormWindowState.Minimized)
            //{
            //  //  this.noAuto.Visible = true;
            //   // this.Hide();
            //    this.Visible = true;
            //}
            //else
            //{
 
            //}
            //MessageBox.Show(this.WindowState.ToString());

        }

 

        #region 还原窗体

        private void normalForm()
        {
            this.Visible = true;
            this.WindowState=FormWindowState.Normal;
        }
        #endregion

 
        private void minForm()
        {
            WindowState = FormWindowState.Minimized;
            this.Visible = false;
            this.noAuto.Visible = true;
        }

        private void noAuto_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized || this.Visible == false)
            {
                this.normalForm();
            }
            else
            {
                minForm();
            }
        }

        private void FormHome_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = false;
            }
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Dirs(this.FbdAllFiles.SelectedPath);
            //MessageBox.Show(this.richTextBox1.Text.Length.ToString());
        }

        private void BtObserve_Click(object sender, EventArgs e)
        {
            string tempUrl = this.TbUrl.Text.Trim();
            string tempType = this.CbType.Text.Trim();
            tempType= tempType.Length > 0 ? (tempType) : ("gb2312");
            this.RtbContent.Text = this.GetHttpData(tempUrl, tempType);

        }
        public string GetHttpData(string sUrl,string encoding)
        {
            string sRslt = null;
            WebResponse oWebRps = null;
            WebRequest oWebRqst = WebRequest.Create(sUrl);
            oWebRqst.Timeout = 50000;
            try
            {
                oWebRps = oWebRqst.GetResponse();
            }

            finally
            {
                if (oWebRps != null)
                {
                    StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), System.Text.Encoding.GetEncoding(encoding));
                    sRslt = oStreamRd.ReadToEnd();
                    oStreamRd.Close();
                    oWebRps.Close();
                }
            }
            return sRslt;
        }
        //public void Dirs(string Path)
        //{

        //    System.IO.DirectoryInfo dirs = new System.IO.DirectoryInfo(Path);
        //    foreach (FileInfo fi in dirs.GetFiles("*.htm"))
        //    {

        //        Process.Start("IExplore.exe", fi.FullName);
        //    }

        //    if (dirs.GetDirectories().Length > 0)
        //    {
        //        foreach (DirectoryInfo di in dirs.GetDirectories())
        //        {
        //            Dirs(di.FullName);
        //        }
        //    }
        //}


        private void BtBrowerFiles_Click(object sender, EventArgs e)
        {
            this.FbdAllFiles.ShowDialog();
        }

        private void BtSearch_Click(object sender, EventArgs e)
        {
            //百度一下,找到相关网页约384,000篇
             string tempUrl = this.TbUrl.Text.Trim();
            string tempType = this.CbType.Text.Trim();
            tempType= tempType.Length > 0 ? (tempType) : ("gb2312");
           // this.RtbContent.Text = this.GetHttpData(tempUrl, tempType);
           Match mat = Regex.Match(GetHttpData(tempUrl, tempType), "百度一下,找到相关网页约(?<ResultCount>.*)篇", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
           MessageBox.Show(mat.Groups["ResultCount"].Value);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //百度一下,找到相关网页约384,000篇
            string tempUrl = this.TbUrl.Text.Trim();
            string tempType = this.CbType.Text.Trim();
            tempType = tempType.Length > 0 ? (tempType) : ("gb2312");
            Match mat = Regex.Match(GetHttpData(tempUrl, tempType), "has a  traffic rank of:(?<ResultCount>.*)<!--", System.Text.RegularExpressions.RegexOptions.IgnoreCase );
            MessageBox.Show(Regex.Replace(mat.Groups["ResultCount"].Value,"<[^>]*>",""));
        }
        public void Dirs(string path)
        {
            DirectoryInfo dis = new DirectoryInfo(path);
            FileInfo[] files = dis.GetFiles();
            foreach (FileInfo fi in files)
            {
                i++;
                Application.DoEvents();
                this.labCount.Text = i.ToString();
                try
                {
                    File.Delete(fi.FullName);
                }
                catch (Exception ex)
                {
                    this.rtbMessage.Text += ex.Message;

                }

                //  GetSqlFile(fi.FullName, this.textBox1.Text + fi.DirectoryName.Substring(2), fi.FullName.Substring(fi.FullName.LastIndexOf("\\") + 1), 1024, 768);
            }
            if (dis.GetDirectories().Length > 0)
            {
                for (int i = 0; i < dis.GetDirectories().Length; i++)
                {
                    Dirs(dis.GetDirectories()[i].FullName);
                }
            }


        }

        private void button6_Click(object sender, EventArgs e)
        {
            Dirs(this.textBox1.Text.Trim());
            Dirs(this.textBox2.Text.Trim());
            Dirs(this.textBox3.Text.Trim());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.FbdSelectDeleteFile.ShowDialog();
            this.textBox1.Text = this.FbdSelectDeleteFile.SelectedPath.Trim();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.FbdSelectDeleteFile.ShowDialog();
            this.textBox2.Text = this.FbdSelectDeleteFile.SelectedPath.Trim();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.FbdSelectDeleteFile.ShowDialog();
            this.textBox3.Text = this.FbdSelectDeleteFile.SelectedPath.Trim();
        }

       


     
       
     
    }
}

posted on 2009-06-08 12:09  王德田  阅读(257)  评论(0编辑  收藏  举报