using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Globalization; namespace DirTool { /**//// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1; private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.Button btnDisposal; int directories = 0; int dirfiles = 0; string subfilename = ""; int diranimalfiles = 0; int dirplantfiles = 0; string sqlstring = ""; string filetype = ""; private System.Windows.Forms.Label lblDisp; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox txtRTime; private System.Windows.Forms.CheckBox chkSubDirectory; private System.Windows.Forms.Button btnDir; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txtFloderPath; private System.Windows.Forms.Label label1; private System.Windows.Forms.ComboBox comboBox1; /**//// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /**//// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.btnDisposal = new System.Windows.Forms.Button(); this.lblDisp = new System.Windows.Forms.Label(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.label3 = new System.Windows.Forms.Label(); this.txtRTime = new System.Windows.Forms.TextBox(); this.chkSubDirectory = new System.Windows.Forms.CheckBox(); this.btnDir = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.txtFloderPath = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // folderBrowserDialog1 // this.folderBrowserDialog1.Description = "请选择要处理的文件目录"; this.folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.MyComputer; this.folderBrowserDialog1.ShowNewFolderButton = false; // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(8, 116); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(672, 312); this.richTextBox1.TabIndex = 8; this.richTextBox1.Text = ""; // // btnDisposal // this.btnDisposal.Location = new System.Drawing.Point(584, 48); this.btnDisposal.Name = "btnDisposal"; this.btnDisposal.Size = new System.Drawing.Size(80, 23); this.btnDisposal.TabIndex = 9; this.btnDisposal.Text = "生成SQL"; this.btnDisposal.Click += new System.EventHandler(this.btnDisposal_Click); // // lblDisp // this.lblDisp.Location = new System.Drawing.Point(16, 434); this.lblDisp.Name = "lblDisp"; this.lblDisp.Size = new System.Drawing.Size(656, 23); this.lblDisp.TabIndex = 10; // // groupBox1 // this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.txtRTime); this.groupBox1.Controls.Add(this.chkSubDirectory); this.groupBox1.Controls.Add(this.btnDir); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.txtFloderPath); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Controls.Add(this.comboBox1); this.groupBox1.Location = new System.Drawing.Point(16, 2); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(544, 104); this.groupBox1.TabIndex = 11; this.groupBox1.TabStop = false; // // label3 // this.label3.Location = new System.Drawing.Point(22, 45); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(96, 14); this.label3.TabIndex = 15; this.label3.Text = "当前系统日期:"; // // txtRTime // this.txtRTime.Location = new System.Drawing.Point(118, 42); this.txtRTime.Name = "txtRTime"; this.txtRTime.Size = new System.Drawing.Size(138, 21); this.txtRTime.TabIndex = 14; this.txtRTime.Text = ""; // // chkSubDirectory // this.chkSubDirectory.Location = new System.Drawing.Point(366, 69); this.chkSubDirectory.Name = "chkSubDirectory"; this.chkSubDirectory.Size = new System.Drawing.Size(88, 24); this.chkSubDirectory.TabIndex = 13; this.chkSubDirectory.Text = "包含子目录"; this.chkSubDirectory.CheckedChanged += new System.EventHandler(this.chkSubDirectory_CheckedChanged); // // btnDir // this.btnDir.Location = new System.Drawing.Point(454, 70); this.btnDir.Name = "btnDir"; this.btnDir.Size = new System.Drawing.Size(72, 23); this.btnDir.TabIndex = 12; this.btnDir.Text = "浏览"; this.btnDir.Click += new System.EventHandler(this.btnDir_Click); // // label2 // this.label2.Location = new System.Drawing.Point(20, 73); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(96, 16); this.label2.TabIndex = 11; this.label2.Text = "处理文件目录:"; // // txtFloderPath // this.txtFloderPath.Location = new System.Drawing.Point(118, 71); this.txtFloderPath.Name = "txtFloderPath"; this.txtFloderPath.Size = new System.Drawing.Size(240, 21); this.txtFloderPath.TabIndex = 10; this.txtFloderPath.Text = ""; // // label1 // this.label1.Location = new System.Drawing.Point(19, 18); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(96, 15); this.label1.TabIndex = 9; this.label1.Text = "合作单位名称:"; // // comboBox1 // this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBox1.Items.AddRange(new object[] { "中国林科院森环所", "环保系统自然保护区生物标本整理整合共享试点", "贡嘎山自然保护区标本整理与信息共享", "高黎贡山自然保护区标本整理与信息共享", "壶瓶山自然保护区生物标本整理整合与共享", "自然保护区民族医药标本整理", "保护区土壤动物标本整理整合与数字化表达", "保护区小型哺乳动物标本整理整合与数字化表达", "湖南八大公山自然保护区标本整理与信息共享", "贵州梵净山自然保护区标本整理与信息共享", "自然保护区标本信息采集整理技术研制", "保护区主要保护物种木材材性标本整理整合与信息共享", "广东湛江红树林自然保护区标本整理", "青海湖保护区标本整理与信息共享", "吉林省自然保护区标本整理与信息共享", "西南林学院", "森林昆虫", "林科院森保所张于光", "林科院森保所金崑"}); this.comboBox1.Location = new System.Drawing.Point(118, 16); this.comboBox1.MaxDropDownItems = 10; this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(408, 20); this.comboBox1.TabIndex = 8; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(690, 464); this.Controls.Add(this.groupBox1); this.Controls.Add(this.lblDisp); this.Controls.Add(this.btnDisposal); this.Controls.Add(this.richTextBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "目录文件转SQl语句工具"; this.Load += new System.EventHandler(this.Form1_Load); this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } #endregion /**//// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void btnDir_Click(object sender, System.EventArgs e) { this.folderBrowserDialog1.ShowDialog(); this.txtFloderPath.Text = this.folderBrowserDialog1.SelectedPath; if(this.folderBrowserDialog1.SelectedPath !="") { this.richTextBox1.Text = ""; directories = 0; dirfiles = 0; //判断是否包含子目录 if(this.chkSubDirectory.Checked == false) { string[] myfiles = System.IO.Directory.GetFiles(this.txtFloderPath.Text.Trim()); //在richText控件中显示目录下的文件 for(int i=0;i<myfiles.Length;i++) { myfiles[i] = myfiles[i].Replace(this.txtFloderPath.Text.Trim()+"\\",""); this.richTextBox1.AppendText(myfiles[i].ToString()+System.Environment.NewLine); dirfiles++; } this.lblDisp.Text = "所选目录共有:"+dirfiles+"个文件"; } else { ListFiles(new DirectoryInfo(this.folderBrowserDialog1.SelectedPath)); this.lblDisp.Text = "所选目录共有:"+directories+" 个子目录"+" 共有:"+dirfiles+"个文件"; } } } private void Form1_Load(object sender, System.EventArgs e) { this.txtRTime.Text = System.DateTime.Today.ToShortDateString(); } private void btnDisposal_Click(object sender, System.EventArgs e) { if(this.txtFloderPath.Text == "") { MessageBox.Show("请选择要处理文件的目录!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } if(this.comboBox1.Text == "") { MessageBox.Show("请选择要合作单位名称!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } this.richTextBox1.Text = ""; try { if(this.txtFloderPath.Text != "") { this.richTextBox1.Text = ""; directories = 0; dirfiles = 0; diranimalfiles = 0; dirplantfiles = 0; //判断是否包含子目录 if(this.chkSubDirectory.Checked == false) { string[] myfiles = System.IO.Directory.GetFiles(this.txtFloderPath.Text.Trim()); //在richText控件中显示目录下的文件 for(int i=0;i<myfiles.Length;i++) { filetype = this.getFileType(myfiles[i]); if(filetype !="") { sqlstring = ""; //insert into User_Task values('壶瓶山自然保护区生物标本整理整合与共享','10.xml','植物','','2007-3-1') myfiles[i] = myfiles[i].Replace(this.txtFloderPath.Text.Trim()+"\\",""); sqlstring = "insert into User_Task values('"+this.comboBox1.Text+"','"+myfiles[i]+"','"+filetype+"','','"+this.txtRTime.Text.Trim()+"')"; this.richTextBox1.AppendText(sqlstring+System.Environment.NewLine); } else { MessageBox.Show("文件:"+myfiles[i]+"不是标本文件","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error); } dirfiles++; } this.lblDisp.Text = "所选目录共有:"+dirfiles+"个文件 动物标本:"+this.diranimalfiles+"个 植物标本:"+this.dirplantfiles+"个"; } else { this.SqlListFiles(new DirectoryInfo(this.folderBrowserDialog1.SelectedPath)); this.lblDisp.Text = "共有:"+directories+" 个子目录"+" 共有:"+dirfiles+"个文件 动物标本:"+this.diranimalfiles+"个 植物标本:"+this.dirplantfiles+"个"; } } } catch(Exception error) { MessageBox.Show("读写文件时出现错误!\r\n错误代码:\r\n"+error.Message,"错误提示",MessageBoxButtons.OK,MessageBoxIcon.Error); } } //遍历当前目录下的所有文件夹河文件生成SQL public void SqlListFiles(FileSystemInfo fileinfo) { if(!fileinfo.Exists) return; DirectoryInfo dirinfo = fileinfo as DirectoryInfo; if(dirinfo == null) return; //不是目录 FileSystemInfo[] files = dirinfo.GetFileSystemInfos(); for(int i=0;i<files.Length;i++) //遍历目录下所有文件、子目录 { FileInfo file = files[i] as FileInfo; if(file != null) // 是文件 { filetype = this.getFileType(file.FullName); if(filetype !="") { sqlstring = ""; //insert into User_Task values('壶瓶山自然保护区生物标本整理整合与共享','10.xml','植物','','2007-3-1') subfilename = file.FullName.Replace(this.txtFloderPath.Text.Trim()+"\\",""); sqlstring = "insert into User_Task values('"+this.comboBox1.Text+"','"+subfilename+"','"+filetype+"','','"+this.txtRTime.Text.Trim()+"')"; this.richTextBox1.AppendText(sqlstring+System.Environment.NewLine); } else { MessageBox.Show("文件:"+file.FullName+"不是标本文件","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error); } dirfiles++; } else //是目录 { directories++; //this.richTextBox1.AppendText(files[i].FullName+System.Environment.NewLine); //显示目录 this.SqlListFiles(files[i]); //对子目录进行递归调用 } } } //检测文件类型 private string getFileType(string filename) { StreamReader sr = new System.IO.StreamReader(filename,System.Text.Encoding.UTF8); try { string currentString = ""; do { currentString = sr.ReadLine(); if(currentString.IndexOf("<ns1:AnimalSpeciman ") >=0) { diranimalfiles++; filetype = "动物"; } else if(currentString.IndexOf("<ns1:PlantSpeciman ")>=0) { dirplantfiles++; filetype = "植物"; } else filetype = ""; }while(currentString==""); sr.Close(); } catch { sr.Close(); } return filetype; } //遍历当前目录下的所有文件夹河文件 public void ListFiles(FileSystemInfo fileinfo) { if(!fileinfo.Exists) return; DirectoryInfo dirinfo = fileinfo as DirectoryInfo; if(dirinfo == null) return; //不是目录 FileSystemInfo[] files = dirinfo.GetFileSystemInfos(); for(int i=0;i<files.Length;i++) //遍历目录下所有文件、子目录 { FileInfo file = files[i] as FileInfo; if(file != null) // 是文件 { subfilename = file.FullName.Replace(this.txtFloderPath.Text.Trim()+"\\",""); this.richTextBox1.AppendText(subfilename+System.Environment.NewLine); dirfiles++; } else //是目录 { directories++; //this.richTextBox1.AppendText(files[i].FullName+System.Environment.NewLine); //显示目录 ListFiles(files[i]); //对子目录进行递归调用 } } } private void chkSubDirectory_CheckedChanged(object sender, System.EventArgs e) { if(this.txtFloderPath.Text != "") { this.richTextBox1.Text = ""; directories = 0; dirfiles = 0; //判断是否包含子目录 if(this.chkSubDirectory.Checked == false) { string[] myfiles = System.IO.Directory.GetFiles(this.txtFloderPath.Text.Trim()); //在richText控件中显示目录下的文件 for(int i=0;i<myfiles.Length;i++) { myfiles[i] = myfiles[i].Replace(this.txtFloderPath.Text.Trim()+"\\",""); this.richTextBox1.AppendText(myfiles[i].ToString()+System.Environment.NewLine); dirfiles++; } this.lblDisp.Text = "所选目录共有:"+dirfiles+"个文件"; } else { ListFiles(new DirectoryInfo(this.folderBrowserDialog1.SelectedPath)); this.lblDisp.Text = "所选目录共有:"+directories+" 个子目录"+" 共有:"+dirfiles+"个文件"; } } } }}