遍历当前目录下的所有子目录例子(自动生成SQl语句工具)


li.jpg
None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
using  System.IO;
None.gif
using  System.Globalization;
None.gif
None.gif
namespace  DirTool
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
InBlock.gif        
private System.Windows.Forms.RichTextBox richTextBox1;
InBlock.gif        
private System.Windows.Forms.Button btnDisposal;
InBlock.gif        
int directories = 0;
InBlock.gif        
int dirfiles = 0;
InBlock.gif        
string subfilename = "";
InBlock.gif        
int diranimalfiles = 0;
InBlock.gif        
int dirplantfiles = 0;
InBlock.gif        
string sqlstring = "";
InBlock.gif        
string filetype = "";
InBlock.gif        
private System.Windows.Forms.Label lblDisp;
InBlock.gif        
private System.Windows.Forms.GroupBox groupBox1;
InBlock.gif        
private System.Windows.Forms.Label label3;
InBlock.gif        
private System.Windows.Forms.TextBox txtRTime;
InBlock.gif        
private System.Windows.Forms.CheckBox chkSubDirectory;
InBlock.gif        
private System.Windows.Forms.Button btnDir;
InBlock.gif        
private System.Windows.Forms.Label label2;
InBlock.gif        
private System.Windows.Forms.TextBox txtFloderPath;
InBlock.gif        
private System.Windows.Forms.Label label1;
InBlock.gif        
private System.Windows.Forms.ComboBox comboBox1;
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Resources.ResourceManager resources 
= new System.Resources.ResourceManager(typeof(Form1));
InBlock.gif            
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
InBlock.gif            
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
InBlock.gif            
this.btnDisposal = new System.Windows.Forms.Button();
InBlock.gif            
this.lblDisp = new System.Windows.Forms.Label();
InBlock.gif            
this.groupBox1 = new System.Windows.Forms.GroupBox();
InBlock.gif            
this.label3 = new System.Windows.Forms.Label();
InBlock.gif            
this.txtRTime = new System.Windows.Forms.TextBox();
InBlock.gif            
this.chkSubDirectory = new System.Windows.Forms.CheckBox();
InBlock.gif            
this.btnDir = new System.Windows.Forms.Button();
InBlock.gif            
this.label2 = new System.Windows.Forms.Label();
InBlock.gif            
this.txtFloderPath = new System.Windows.Forms.TextBox();
InBlock.gif            
this.label1 = new System.Windows.Forms.Label();
InBlock.gif            
this.comboBox1 = new System.Windows.Forms.ComboBox();
InBlock.gif            
this.groupBox1.SuspendLayout();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// folderBrowserDialog1
InBlock.gif            
// 
InBlock.gif
            this.folderBrowserDialog1.Description = "请选择要处理的文件目录";
InBlock.gif            
this.folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.MyComputer;
InBlock.gif            
this.folderBrowserDialog1.ShowNewFolderButton = false;
InBlock.gif            
// 
InBlock.gif            
// richTextBox1
InBlock.gif            
// 
InBlock.gif
            this.richTextBox1.Location = new System.Drawing.Point(8116);
InBlock.gif            
this.richTextBox1.Name = "richTextBox1";
InBlock.gif            
this.richTextBox1.Size = new System.Drawing.Size(672312);
InBlock.gif            
this.richTextBox1.TabIndex = 8;
InBlock.gif            
this.richTextBox1.Text = "";
InBlock.gif            
// 
InBlock.gif            
// btnDisposal
InBlock.gif            
// 
InBlock.gif
            this.btnDisposal.Location = new System.Drawing.Point(58448);
InBlock.gif            
this.btnDisposal.Name = "btnDisposal";
InBlock.gif            
this.btnDisposal.Size = new System.Drawing.Size(8023);
InBlock.gif            
this.btnDisposal.TabIndex = 9;
InBlock.gif            
this.btnDisposal.Text = "生成SQL";
InBlock.gif            
this.btnDisposal.Click += new System.EventHandler(this.btnDisposal_Click);
InBlock.gif            
// 
InBlock.gif            
// lblDisp
InBlock.gif            
// 
InBlock.gif
            this.lblDisp.Location = new System.Drawing.Point(16434);
InBlock.gif            
this.lblDisp.Name = "lblDisp";
InBlock.gif            
this.lblDisp.Size = new System.Drawing.Size(65623);
InBlock.gif            
this.lblDisp.TabIndex = 10;
InBlock.gif            
// 
InBlock.gif            
// groupBox1
InBlock.gif            
// 
InBlock.gif
            this.groupBox1.Controls.Add(this.label3);
InBlock.gif            
this.groupBox1.Controls.Add(this.txtRTime);
InBlock.gif            
this.groupBox1.Controls.Add(this.chkSubDirectory);
InBlock.gif            
this.groupBox1.Controls.Add(this.btnDir);
InBlock.gif            
this.groupBox1.Controls.Add(this.label2);
InBlock.gif            
this.groupBox1.Controls.Add(this.txtFloderPath);
InBlock.gif            
this.groupBox1.Controls.Add(this.label1);
InBlock.gif            
this.groupBox1.Controls.Add(this.comboBox1);
InBlock.gif            
this.groupBox1.Location = new System.Drawing.Point(162);
InBlock.gif            
this.groupBox1.Name = "groupBox1";
InBlock.gif            
this.groupBox1.Size = new System.Drawing.Size(544104);
InBlock.gif            
this.groupBox1.TabIndex = 11;
InBlock.gif            
this.groupBox1.TabStop = false;
InBlock.gif            
// 
InBlock.gif            
// label3
InBlock.gif            
// 
InBlock.gif
            this.label3.Location = new System.Drawing.Point(2245);
InBlock.gif            
this.label3.Name = "label3";
InBlock.gif            
this.label3.Size = new System.Drawing.Size(9614);
InBlock.gif            
this.label3.TabIndex = 15;
InBlock.gif            
this.label3.Text = "当前系统日期:";
InBlock.gif            
// 
InBlock.gif            
// txtRTime
InBlock.gif            
// 
InBlock.gif
            this.txtRTime.Location = new System.Drawing.Point(11842);
InBlock.gif            
this.txtRTime.Name = "txtRTime";
InBlock.gif            
this.txtRTime.Size = new System.Drawing.Size(13821);
InBlock.gif            
this.txtRTime.TabIndex = 14;
InBlock.gif            
this.txtRTime.Text = "";
InBlock.gif            
// 
InBlock.gif            
// chkSubDirectory
InBlock.gif            
// 
InBlock.gif
            this.chkSubDirectory.Location = new System.Drawing.Point(36669);
InBlock.gif            
this.chkSubDirectory.Name = "chkSubDirectory";
InBlock.gif            
this.chkSubDirectory.Size = new System.Drawing.Size(8824);
InBlock.gif            
this.chkSubDirectory.TabIndex = 13;
InBlock.gif            
this.chkSubDirectory.Text = "包含子目录";
InBlock.gif            
this.chkSubDirectory.CheckedChanged += new System.EventHandler(this.chkSubDirectory_CheckedChanged);
InBlock.gif            
// 
InBlock.gif            
// btnDir
InBlock.gif            
// 
InBlock.gif
            this.btnDir.Location = new System.Drawing.Point(45470);
InBlock.gif            
this.btnDir.Name = "btnDir";
InBlock.gif            
this.btnDir.Size = new System.Drawing.Size(7223);
InBlock.gif            
this.btnDir.TabIndex = 12;
InBlock.gif            
this.btnDir.Text = "浏览dot.gif";
InBlock.gif            
this.btnDir.Click += new System.EventHandler(this.btnDir_Click);
InBlock.gif            
// 
InBlock.gif            
// label2
InBlock.gif            
// 
InBlock.gif
            this.label2.Location = new System.Drawing.Point(2073);
InBlock.gif            
this.label2.Name = "label2";
InBlock.gif            
this.label2.Size = new System.Drawing.Size(9616);
InBlock.gif            
this.label2.TabIndex = 11;
InBlock.gif            
this.label2.Text = "处理文件目录:";
InBlock.gif            
// 
InBlock.gif            
// txtFloderPath
InBlock.gif            
// 
InBlock.gif
            this.txtFloderPath.Location = new System.Drawing.Point(11871);
InBlock.gif            
this.txtFloderPath.Name = "txtFloderPath";
InBlock.gif            
this.txtFloderPath.Size = new System.Drawing.Size(24021);
InBlock.gif            
this.txtFloderPath.TabIndex = 10;
InBlock.gif            
this.txtFloderPath.Text = "";
InBlock.gif            
// 
InBlock.gif            
// label1
InBlock.gif            
// 
InBlock.gif
            this.label1.Location = new System.Drawing.Point(1918);
InBlock.gif            
this.label1.Name = "label1";
InBlock.gif            
this.label1.Size = new System.Drawing.Size(9615);
InBlock.gif            
this.label1.TabIndex = 9;
InBlock.gif            
this.label1.Text = "合作单位名称:";
InBlock.gif            
// 
InBlock.gif            
// comboBox1
InBlock.gif            
// 
InBlock.gif
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.comboBox1.Items.AddRange(new object[] dot.gif{
InBlock.gif                                                           
"中国林科院森环所",
InBlock.gif                                                           
"环保系统自然保护区生物标本整理整合共享试点",
InBlock.gif                                                           
"贡嘎山自然保护区标本整理与信息共享",
InBlock.gif                                                           
"高黎贡山自然保护区标本整理与信息共享",
InBlock.gif                                                           
"壶瓶山自然保护区生物标本整理整合与共享",
InBlock.gif                                                           
"自然保护区民族医药标本整理",
InBlock.gif                                                           
"保护区土壤动物标本整理整合与数字化表达",
InBlock.gif                                                           
"保护区小型哺乳动物标本整理整合与数字化表达",
InBlock.gif                                                           
"湖南八大公山自然保护区标本整理与信息共享",
InBlock.gif                                                           
"贵州梵净山自然保护区标本整理与信息共享",
InBlock.gif                                                           
"自然保护区标本信息采集整理技术研制",
InBlock.gif                                                           
"保护区主要保护物种木材材性标本整理整合与信息共享",
InBlock.gif                                                           
"广东湛江红树林自然保护区标本整理",
InBlock.gif                                                           
"青海湖保护区标本整理与信息共享",
InBlock.gif                                                           
"吉林省自然保护区标本整理与信息共享",
InBlock.gif                                                           
"西南林学院",
InBlock.gif                                                           
"森林昆虫",
InBlock.gif                                                           
"林科院森保所张于光",
ExpandedSubBlockEnd.gif                                                           
"林科院森保所金崑"}
);
InBlock.gif            
this.comboBox1.Location = new System.Drawing.Point(11816);
InBlock.gif            
this.comboBox1.MaxDropDownItems = 10;
InBlock.gif            
this.comboBox1.Name = "comboBox1";
InBlock.gif            
this.comboBox1.Size = new System.Drawing.Size(40820);
InBlock.gif            
this.comboBox1.TabIndex = 8;
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(690464);
InBlock.gif            
this.Controls.Add(this.groupBox1);
InBlock.gif            
this.Controls.Add(this.lblDisp);
InBlock.gif            
this.Controls.Add(this.btnDisposal);
InBlock.gif            
this.Controls.Add(this.richTextBox1);
InBlock.gif            
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
InBlock.gif            
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
InBlock.gif            
this.MaximizeBox = false;
InBlock.gif            
this.MinimizeBox = false;
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
InBlock.gif            
this.Text = "目录文件转SQl语句工具";
InBlock.gif            
this.Load += new System.EventHandler(this.Form1_Load);
InBlock.gif            
this.groupBox1.ResumeLayout(false);
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnDir_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            
this.folderBrowserDialog1.ShowDialog();
InBlock.gif            
this.txtFloderPath.Text = this.folderBrowserDialog1.SelectedPath;
InBlock.gif
InBlock.gif            
if(this.folderBrowserDialog1.SelectedPath !="")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.richTextBox1.Text = "";
InBlock.gif                directories 
= 0;
InBlock.gif                dirfiles 
= 0;
InBlock.gif            
InBlock.gif                
//判断是否包含子目录
InBlock.gif
                if(this.chkSubDirectory.Checked == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string[] myfiles = System.IO.Directory.GetFiles(this.txtFloderPath.Text.Trim());                
InBlock.gif                
InBlock.gif                    
//在richText控件中显示目录下的文件
InBlock.gif
                    for(int i=0;i<myfiles.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        myfiles[i] 
= myfiles[i].Replace(this.txtFloderPath.Text.Trim()+"\\","");
InBlock.gif                        
this.richTextBox1.AppendText(myfiles[i].ToString()+System.Environment.NewLine);    
InBlock.gif                        dirfiles
++;
ExpandedSubBlockEnd.gif                    }
    
InBlock.gif                    
this.lblDisp.Text = "所选目录共有:"+dirfiles+"个文件";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                
InBlock.gif                    ListFiles(
new  DirectoryInfo(this.folderBrowserDialog1.SelectedPath)); 
InBlock.gif                    
this.lblDisp.Text = "所选目录共有:"+directories+" 个子目录"+"   共有:"+dirfiles+"个文件";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.txtRTime.Text = System.DateTime.Today.ToShortDateString();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
InBlock.gif        
private void btnDisposal_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
if(this.txtFloderPath.Text == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"请选择要处理文件的目录!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); 
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if(this.comboBox1.Text == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"请选择要合作单位名称!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); 
InBlock.gif                
return;                
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
this.richTextBox1.Text = "";
InBlock.gif
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
if(this.txtFloderPath.Text != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.richTextBox1.Text = "";
InBlock.gif                    directories 
= 0;
InBlock.gif                    dirfiles 
= 0;
InBlock.gif                    diranimalfiles 
= 0;
InBlock.gif                    dirplantfiles 
= 0;
InBlock.gif            
InBlock.gif                    
//判断是否包含子目录
InBlock.gif
                    if(this.chkSubDirectory.Checked == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
string[] myfiles = System.IO.Directory.GetFiles(this.txtFloderPath.Text.Trim());                
InBlock.gif                
InBlock.gif                        
//在richText控件中显示目录下的文件
InBlock.gif
                        for(int i=0;i<myfiles.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            filetype 
= this.getFileType(myfiles[i]);
InBlock.gif                            
if(filetype !="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                sqlstring 
= "";
InBlock.gif                                
//insert into User_Task values('壶瓶山自然保护区生物标本整理整合与共享','10.xml','植物','','2007-3-1')
InBlock.gif
                                myfiles[i] = myfiles[i].Replace(this.txtFloderPath.Text.Trim()+"\\","");
InBlock.gif                                sqlstring 
= "insert into User_Task values('"+this.comboBox1.Text+"','"+myfiles[i]+"','"+filetype+"','','"+this.txtRTime.Text.Trim()+"')";
InBlock.gif                                
this.richTextBox1.AppendText(sqlstring+System.Environment.NewLine);
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                MessageBox.Show(
"文件:"+myfiles[i]+"不是标本文件","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            dirfiles
++;
ExpandedSubBlockEnd.gif                        }
                            
InBlock.gif                        
this.lblDisp.Text = "所选目录共有:"+dirfiles+"个文件  动物标本:"+this.diranimalfiles+"个  植物标本:"+this.dirplantfiles+"";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{                
InBlock.gif                        
this.SqlListFiles(new  DirectoryInfo(this.folderBrowserDialog1.SelectedPath)); 
InBlock.gif                        
this.lblDisp.Text = "共有:"+directories+" 个子目录"+" 共有:"+dirfiles+"个文件  动物标本:"+this.diranimalfiles+"个  植物标本:"+this.dirplantfiles+"";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif
InBlock.gif                                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception error)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"读写文件时出现错误!\r\n错误代码:\r\n"+error.Message,"错误提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
ExpandedSubBlockEnd.gif            }
            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//遍历当前目录下的所有文件夹河文件生成SQL
InBlock.gif
        public void SqlListFiles(FileSystemInfo fileinfo)          
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            
if(!fileinfo.Exists) 
InBlock.gif                
return;          
InBlock.gif            DirectoryInfo dirinfo 
= fileinfo as DirectoryInfo;
InBlock.gif                     
InBlock.gif            
if(dirinfo == null)
InBlock.gif                
return;   //不是目录                           
InBlock.gif
          
InBlock.gif            FileSystemInfo[] files 
= dirinfo.GetFileSystemInfos();          
InBlock.gif            
for(int i=0;i<files.Length;i++//遍历目录下所有文件、子目录               
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                FileInfo file 
= files[i] as FileInfo; 
InBlock.gif                
if(file != null)   //   是文件     
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    filetype 
= this.getFileType(file.FullName);
InBlock.gif                    
if(filetype !="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sqlstring 
= "";
InBlock.gif                        
//insert into User_Task values('壶瓶山自然保护区生物标本整理整合与共享','10.xml','植物','','2007-3-1')                        
InBlock.gif
                        subfilename = file.FullName.Replace(this.txtFloderPath.Text.Trim()+"\\","");
InBlock.gif
InBlock.gif                        sqlstring 
= "insert into User_Task values('"+this.comboBox1.Text+"','"+subfilename+"','"+filetype+"','','"+this.txtRTime.Text.Trim()+"')";
InBlock.gif                        
this.richTextBox1.AppendText(sqlstring+System.Environment.NewLine);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        MessageBox.Show(
"文件:"+file.FullName+"不是标本文件","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    dirfiles
++;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else   //是目录
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    directories
++;
InBlock.gif                    
//this.richTextBox1.AppendText(files[i].FullName+System.Environment.NewLine);  //显示目录
InBlock.gif
                    this.SqlListFiles(files[i]);   //对子目录进行递归调用
ExpandedSubBlockEnd.gif
                }
                                                         
ExpandedSubBlockEnd.gif            }
        
ExpandedSubBlockEnd.gif        }
  
InBlock.gif
InBlock.gif
InBlock.gif        
InBlock.gif        
//检测文件类型
InBlock.gif
        private string getFileType(string filename)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            StreamReader sr 
= new System.IO.StreamReader(filename,System.Text.Encoding.UTF8);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string currentString = "";                
InBlock.gif                
do
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    currentString 
= sr.ReadLine();
InBlock.gif                    
if(currentString.IndexOf("<ns1:AnimalSpeciman ">=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        diranimalfiles
++;                        
InBlock.gif                        filetype 
= "动物";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if(currentString.IndexOf("<ns1:PlantSpeciman ")>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{                    
InBlock.gif                        dirplantfiles
++;
InBlock.gif                        filetype 
= "植物";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
InBlock.gif                         filetype 
= "";
ExpandedSubBlockEnd.gif                }
while(currentString=="");
InBlock.gif                sr.Close();
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sr.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return filetype;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
InBlock.gif        
//遍历当前目录下的所有文件夹河文件
InBlock.gif
        public void ListFiles(FileSystemInfo fileinfo)          
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            
if(!fileinfo.Exists) 
InBlock.gif                
return;          
InBlock.gif            DirectoryInfo dirinfo 
= fileinfo as DirectoryInfo;
InBlock.gif                     
InBlock.gif            
if(dirinfo == null)
InBlock.gif                
return;   //不是目录                           
InBlock.gif
          
InBlock.gif            FileSystemInfo[] files 
= dirinfo.GetFileSystemInfos();          
InBlock.gif            
for(int i=0;i<files.Length;i++//遍历目录下所有文件、子目录               
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                FileInfo file 
= files[i] as FileInfo; 
InBlock.gif                
if(file != null)   //   是文件     
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    subfilename 
= file.FullName.Replace(this.txtFloderPath.Text.Trim()+"\\","");
InBlock.gif                    
this.richTextBox1.AppendText(subfilename+System.Environment.NewLine);
InBlock.gif                    dirfiles
++;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else   //是目录
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    directories
++;
InBlock.gif                    
//this.richTextBox1.AppendText(files[i].FullName+System.Environment.NewLine);  //显示目录
InBlock.gif
                    ListFiles(files[i]);   //对子目录进行递归调用
ExpandedSubBlockEnd.gif
                }
                                                         
ExpandedSubBlockEnd.gif            }
        
ExpandedSubBlockEnd.gif        }
  
InBlock.gif
InBlock.gif        
private void chkSubDirectory_CheckedChanged(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(this.txtFloderPath.Text != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.richTextBox1.Text = "";
InBlock.gif                directories 
= 0;
InBlock.gif                dirfiles 
= 0;
InBlock.gif            
InBlock.gif                
//判断是否包含子目录
InBlock.gif
                if(this.chkSubDirectory.Checked == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string[] myfiles = System.IO.Directory.GetFiles(this.txtFloderPath.Text.Trim());                
InBlock.gif                
InBlock.gif                    
//在richText控件中显示目录下的文件
InBlock.gif
                    for(int i=0;i<myfiles.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        myfiles[i] 
= myfiles[i].Replace(this.txtFloderPath.Text.Trim()+"\\","");
InBlock.gif                        
this.richTextBox1.AppendText(myfiles[i].ToString()+System.Environment.NewLine);
InBlock.gif                        dirfiles
++;
ExpandedSubBlockEnd.gif                    }
    
InBlock.gif                    
this.lblDisp.Text = "所选目录共有:"+dirfiles+"个文件";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                
InBlock.gif                    ListFiles(
new  DirectoryInfo(this.folderBrowserDialog1.SelectedPath)); 
InBlock.gif                    
this.lblDisp.Text = "所选目录共有:"+directories+" 个子目录"+"   共有:"+dirfiles+"个文件";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif        
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值