using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DeleteFilesNoNeed
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

         
            zj123.SQLDB.DbAccess dba = new zj123.SQLDB.DbAccess(this.textBox2.Text.Trim());
            //插入临时表数据--对应年月的
           // string tempInsertSql = "insert into tempDataFiles([FileName]) select [FileName] from datafiles where [filename] like '%"+this.textBox1.Text.Trim()+"%'";
           // string tempDeleteSql = "delete from tempDataFiles";
            //dba.GetState(tempDeleteSql + ";" + tempInsertSql + ";");
            //dba.GetState(tempDeleteSql);
            StringBuilder sbsql = new StringBuilder("delete from DriveFiles;");
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(this.folderBrowserDialog1.SelectedPath);

            foreach (System.IO.FileInfo fi in dir.GetFiles())
            {
                sbsql.AppendFormat("insert into DriveFiles([FileName],[FilePath]) values('{0}','{1}');",fi.Name,fi.FullName);

            }
            foreach(System.IO.DirectoryInfo di in dir.GetDirectories())
            {
                foreach (System.IO.FileInfo fi in di.GetFiles())
                {
                    sbsql.AppendFormat("insert into DriveFiles([FileName],[FilePath]) values('{0}','{1}');", fi.Name, fi.FullName);

                }

            }
            dba.GetState(sbsql.ToString());
            string tempDeletSql = "select * from DriveFiles where ltrim(rtrim([FileName])) not in(select ltrim(rtrim([FileName])) from tempDataFiles)";
            DataTable dt = dba.GetTable(tempDeletSql);
            int i = 0;
            int a = dt.Rows.Count;
           // StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                System.IO.File.Delete(Convert.ToString(dr["FilePath"]).Trim());
             //   sb.Append(Convert.ToString(dr["FilePath"]).Trim()).Append(System.Environment.NewLine);
                i++;
                this.label4.Text = "正在删除" + i.ToString()+"---共"+a.ToString().ToString();
                Application.DoEvents();
            }
           // this.richTextBox1.Text = sb.ToString();

        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string filePath = this.folderBrowserDialog1.SelectedPath;

        }
    }
}

posted on 2009-06-03 11:45  bestsaler  阅读(209)  评论(0编辑  收藏  举报