遍历当前目录下的所有子目录例子(自动生成SQl语句工具)
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+"个文件";
}
}
}
}
}
转载于:https://www.cnblogs.com/Dragon-China/archive/2007/03/06/665228.html
遍历当前目录下的所有子目录例子(自动生成SQl语句工具)相关推荐
- NO2:自动生成sql语句
SQL语句自动生成工具 大哉乾元 2016/2/26 作者原创转载请注明出处 前言 这个程序是几年前做成的,现在整理成文档和大家分享,当时参与的项目中大量使用的sql语句,所以SqL语句的代码输入 ...
- 用excel自动生成sql语句
在空白列插入函数 =CONCATENATE("insert into user(code, name) values('",A1, "','", B1, &qu ...
- java自动生成sql语句
java自动生成sql语句 艳学网强势来袭http://47.98.237.162/index,首次发布艳辉工具,第一个工具我们发布sql语句自动生成. 以前首次接触sql是在大学期间,刚开始是手写s ...
- 解放程序员双手!GPT-3自动生成SQL语句 | 代码开源
金磊 发自 凹非寺 量子位 报道 | 公众号 QbitAI "无所不能"的GPT-3,现在又来解放程序员们的双手了. 像这样,只需用简单的英文问下GPT-3"上个月注册了 ...
- android studio 自动生成sql语句,Android Studio Plugin 插件开发教程(三) —— 制作一个自动生成数据库代码的插件...
项目源码 系列教程 插件介绍 本篇实战撸个自动生成安卓Sqlite数据库代码的插件,先演示下最终效果 db文件夹下的都是插件自动生成的,而MainActivity里面的代码是我提前写好的,用于实验插件 ...
- 使用Excel自动生成sql语句
在近一段日子里,进入了新的项目组,由于项目需要,经常要将一些Excel表中的数据导入数据库中,以前并没有过多的接触过数据导入与数据处理,对于我来说比较痛苦,今天下午花了几个小时处理数据,但是同事给我提 ...
- mysql逆向工程封装_MyBatis高级应用之逆向工程自动生成SQL语句
本文以Spring整合MyBatis环境下,介绍MyBatis逆向工程自动生成pojo,mapper,dao的Java类.MyBatis的一个主要的特点就是需要程序员自己编写sql,那么如果表太多的话 ...
- java注解的开发_使用Java注解开发自动生成SQL
使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...
- 自动生成SQL查询、删除、更新、插入语句
自动生成sql语句 select 'update ' || t.table_name || ' a set ' || (select wm_concat('a.' || a.colu ...
最新文章
- 高通CVPR神研究:视频处理计算量降低78%,教卷积层自己“挑像素”,卡成PPT的视频纵享丝滑...
- 第三日:继续恢复网站
- 【HDOJ】2510 符号三角形
- android 动画 最顶层_【Android编程实战】StrandHogg漏洞复现及原理分析_Android系统上的维京海盗...
- 归纳+记忆:让机器像人一样从小样本中学习
- 知道自己想要什么,保持自己的节奏。
- Xshell替代品 -- FinalShell
- 每个Java学习者都会犯的10大常见错误1
- 急急急 大神帮忙给个思路和步骤吧 万分感谢
- 正则表达式学习(2)
- python简单爬虫代码-使用Python3.5写简单网络爬虫
- mysql传参为数组,将数组传递给MySQL存储例程
- 计算机毕业设计、课程设计、实战项目之[含论文+答辩PPT+任务书+中期检查表+源码等]基于ssm的NBA球队管理系统
- linux codeblocks汉化
- android键盘表情流畅切换实现
- 反脆弱性—露天广场与办公楼
- 【小白刷题之路Day26】令人虎躯一震的代码
- Client-Initiated场景下的L2TP实验配置
- video视频设置第一帧为封面
- 微信营销的总纲领:沐风微信水库理论 2.0
热门文章
- 4412 GPIO读 和 ioremap控制GPIO寄存器
- JS在即将离开当前页面(刷新或关闭)时触发事件
- BZOJ 2257: [Jsoi2009]瓶子和燃料【数论:裴蜀定理】
- mysql 服务器监控系列-黄杉 mysqldba
- java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
- 这样的“牛”人,绝佳客户最好能多碰上上几个是我们当程序员的好运
- 对付惰性必杀:(10+2)*5法
- @Service注解的使用
- android核心机制之Zygote启动流程
- 从HarmonyOS应用到底是不是Android套壳?