C#磁盘类型判断 所有磁盘遍历出所有文件夹及文件夹路径 所有文件及文件路径

一键删除所有硬盘(Fix  不包含移动盘和光驱)上的 指定文件夹。间隔多长时间执行一次。可以最小化到托盘,可以注册。

C#磁盘类型判断 所有磁盘遍历出所有文件夹及文件夹路径 所有文件及文件路径_第1张图片

C#磁盘类型判断 所有磁盘遍历出所有文件夹及文件夹路径 所有文件及文件路径_第2张图片

开始:开始一键删除所有硬盘(Fix  不包含移动盘和光驱)上的 指定文件夹。间隔多长时间执行一次。

停止:就停止删。

可以最小化到托盘,可以注册。



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

namespace 注册机
{
    public partial class Form1 : Form
    {
        public string fileName = "配置11.txt";
        public Form1()
        {
            InitializeComponent();
        }
        SoftReg softReg = new SoftReg();
        private void Form1_Load(object sender, EventArgs e)
        {
            string str=Read(fileName);
            if (str==null)
            {
                this.textBoxInterval.Text = "36000";
                this.textBoxPath.Text = "示例#test123456";
            }
            else
            {
                string[] strArr = str.Split('*');
                if (strArr.Length>=2)
                {
                    this.textBoxInterval.Text = strArr[strArr.Length-2]; //36000
                    this.textBoxPath.Text = strArr[strArr.Length-1];// "示例#test123456";
                }
            } 
   
            try
            {
                //判断软件是否注册
                RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI");
                foreach (string strRNum in retkey.GetSubKeyNames())
                {
                    if (strRNum == softReg.GetRNum())
                    {
                        this.labRegInfo.Text = "此软件已注册!";
                        this.btnReg.Enabled = false;
                        return;
                    }
                }
                this.labRegInfo.Text = "此软件尚未注册!";
                this.btnReg.Enabled = true;



                //MessageBox.Show("您现在使用的是试用版,可以免费试用5次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                //Int32 tLong;    //已使用次数
                //try
                //{
                //    tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0);
                //    MessageBox.Show("您已经使用了" + tLong + "次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
                //catch
                //{
                //    MessageBox.Show("欢迎使用本软件!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0, RegistryValueKind.DWord);
                //}
                ////判断是否可以继续试用
                //tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0);
                //if (tLong < 5)
                //{
                //    int tTimes = tLong + 1;
                //    Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", tTimes);
                //}
                //else
                //{
                //    DialogResult result = MessageBox.Show("试用次数已到!您是否需要注册?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                //    if (result == DialogResult.Yes)
                //    {
                //        FormRegister.state = false; //设置软件状态为不可用
                //        btnReg_Click(sender, e);    //打开注册窗口
                //    }
                //    else
                //    {
                //        Application.Exit();
                //    }
                //}

                DialogResult result = MessageBox.Show("此软件尚未注册!您是否需要注册?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (result == DialogResult.Yes)
                {
                    FormRegister.state = false; //设置软件状态为不可用
                    btnReg_Click(sender, e);    //打开注册窗口
                }
                else
                {
                    Application.Exit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("请以管理员身份运行!");
                this.Close();
            }

    
        }

        private void btnReg_Click(object sender, EventArgs e)
        {
            FormRegister frmRegister = new FormRegister();
            frmRegister.ShowDialog();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void limited()
        {
            DateTime dt1 = DateTime.Parse("2016-05-20");
            if (DateTime.Compare(dt1, DateTime.Now) < 0)
            {
                MessageBox.Show("试用版已经到期,请联系小黄人软件QQ345139427");
                return;
            }
        }
        private void buttonDel_Click(object sender, EventArgs e)
        {



            limited();
            if (timer1.Enabled)
            {
                timer1.Enabled = false;//设置为truetimer1_Tick实践就会执行,开始计时
                buttonDel.Text = "开始";
            }
            else
            {
                Write(fileName, this.textBoxInterval.Text + "*" + this.textBoxPath.Text);
                timer1.Interval = Convert.ToInt32(this.textBoxInterval.Text);//设置timer1的timer1_Tick实践执行周期为N毫秒
                timer1.Enabled = true;//设置为truetimer1_Tick实践就会执行,开始计时
                buttonDel.Text = "停止";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {



            timer1.Interval = Convert.ToInt32(this.textBoxInterval.Text);//设置timer1的timer1_Tick实践执行周期为N毫秒
    
            DriveInfo[] drives = DriveInfo.GetDrives();
            for (int i = 0; i < drives.Length; i++)
            {
                if (drives[i].DriveType == DriveType.Fixed)
                {
                    ForEachDisk(drives[i].ToString());
                }
            }
           // MessageBox.Show("遍历完毕");
            this.notifyIcon.ShowBalloonTip(10, this.notifyIcon.BalloonTipTitle,this.notifyIcon.BalloonTipText,ToolTipIcon.Info);//
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
        public void Write(string path, string contents)  //写入文件path:E:\\test.txt  内容
        {
            try
            {
            FileStream fs = new FileStream(path, FileMode.Create);  //Create删除该文件,然后创建新文件,不存在则创建  ,FileMode.Append追加
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(contents);  //开始写入 
            sw.Flush(); //清空缓冲区
            sw.Close();//关闭流
            fs.Close();
                    }
            catch (Exception ex)
            {
              //  sw.Close();//关闭流
              //  fs.Close();
                MessageBox.Show("错误提示:" + ex.Message);
            }
        }
        //读文件
        public string Read(string filePath)
        {
            string line = "", contents = "";
            StreamReader sr=null;
            try
            {
               // using (StreamReader sr = new StreamReader(filePath, System.Text.Encoding.GetEncoding("gb2312")))
                sr = new StreamReader(filePath, Encoding.UTF8);              
                while ((line = sr.ReadLine()) != null)
                {
                    contents += line.ToString();
                }
                sr.Close();
            }
            catch (Exception ex)
            {
              
                return null;
            }
      
            return contents;
        }
        private void ForEachDisk(string path)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            try
            {
                foreach (DirectoryInfo d in dir.GetDirectories())
                {
                    if (d.Name.Substring(0, 1) != "$" && d.Name != "System Volume Information")
                    {
                        ForEachDisk(d.FullName);

                        Model m = new Model();
                        m.name = d.Name;
                        m.path = d.FullName;
                        //listBox1.Items.Add(d.Name);  //文件夹名
                        //listBox2.Items.Add(d.FullName);  //文件夹全路径

             
                        string[] s = textBoxPath.Text.Split(new char[] { '#' });
                        for (int i = 0; i < s.Length; i++)
                        {
                            if (d.Name == s[i])
                            {
                                Directory.Delete(d.FullName, true);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("错误提示:统计信息可能不完善。" + ex.Message);
                return;
            }

            foreach (FileInfo f in dir.GetFiles())
            {
                Model m = new Model();
                m.name = f.Name;
                m.path = f.FullName;
                //listBox3.Items.Add(f.Name);   //文件名
                //listBox4.Items.Add(f.FullName); //文件名全路径
            }
         }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized) //判断是否最小化
            {
                this.ShowInTaskbar = false; //不显示在系统任务栏
                notifyIcon.Visible = true; //托盘图标可见
            }
        }

        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                notifyIcon.Visible = false;
                this.ShowInTaskbar = true;
            }
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }
            

    }
    public class Model
    {
        private string _name;
        //private string _type;
        private string _path;

        public string name { get { return _name; } set { _name = value; } }
        //public string type { get { return _type; } set { _type = value; } }
        public string path { get { return _path; } set { _path = value; } }
    }
}

DriveType 属性指示驱动器是否是以下值之一:  CDRom,  Fixed,  Network,  NoRootDirectory,  Ram,  Removable,或 Unknown在将描述这些值DriveType枚举。


            DriveInfo[] s = DriveInfo.GetDrives();
            foreach (DriveInfo i in s)
            {
                if (i.DriveType == DriveType.Removable)
                {
                    MessageBox.Show("发现U盘或移动硬盘");
                }
            }


你可能感兴趣的:(C#磁盘类型判断 所有磁盘遍历出所有文件夹及文件夹路径 所有文件及文件路径)