C# 下载 解压 选择文件夹 保存文件 删除目录下文件及文件夹

//写代码前 先在工具箱里 拉几个按钮和 textbox

//再给几个按钮添加相应的事件,把各个事件里的代码考进去。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Diagnostics;
using Microsoft.Win32;
using System.IO;
namespace updownload
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //下载文件源码
            MessageBox.Show("开始下载");
            try
            {
                System.Net.WebClient myWebClient = new System.Net.WebClient();
                myWebClient.DownloadFile(textBox1.Text.ToString(), textBox2.Text.ToString());

//textbox1和2分别是下载的URL和下载后存放的名字。
            }
            catch
            {
                MessageBox.Show("程序异常");
            }
            MessageBox.Show("结束下载");
             

        }

        private void button2_Click(object sender, EventArgs e)
        {
             string rar;
             RegistryKey reg;
             string args;
             ProcessStartInfo startInfo;
             Process process;
             if (textBox4.Text.Length == 0) 
             {
                 MessageBox.Show("请选择解压文件"); return;
             }
             if (textBox3.Text.Length == 0)
             {
                 MessageBox.Show("请选择存储的位置"); return;
             }
             try
             {
              reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRar.exe/Shell/Open/Command");
              rar = reg.GetValue("").ToString();
              reg.Close();
              rar = rar.Substring(1, rar.Length - 7);
              args = " X -o+  " + "/"" + textBox4.Text + "/"" + "  /"" + textBox3.Text+"/"";
              MessageBox.Show(args);
              startInfo = new ProcessStartInfo();
              startInfo.FileName = rar;
              startInfo.Arguments = args;
              startInfo.WindowStyle = ProcessWindowStyle.Hidden;
              process = new Process();
              process.StartInfo = startInfo;
              process.Start();
              MessageBox.Show("解压成功");
             }
             catch (Exception ex)
             {
            
             }

           
           }

        private void button3_Click(object sender, EventArgs e)
        {
             OpenFileDialog ofDlg = new OpenFileDialog();
             ofDlg.Filter = "text format.rar|*.rar";            
           DialogResult dRet=ofDlg.ShowDialog();
           if(dRet == DialogResult.OK)  
           {  
               string strFile = ofDlg.FileName;  
               textBox4.Text = strFile;               }  
           else 
            {  
                textBox4.Text = "";  
            } 
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //SaveFileDialog loSaveFile = new SaveFileDialog();
            //loSaveFile.Filter = "PDF文件(*.pdf)|*.pdf";
            //loSaveFile.Title = "保存PDF文档";
            //if (loSaveFile.ShowDialog() == DialogResult.OK)
            //{
            //   textBox3.Text = loSaveFile.FileName;
            //}
            SaveFile();
            FolderBrowserDialog folderpath = new FolderBrowserDialog();
            folderpath.Description = "You want selet folder";
            folderpath.ShowDialog();
            textBox3.Text = folderpath.SelectedPath;

        }

        private void button5_Click(object sender, EventArgs e)
        {
           

            //string deleteFileName = "_desktop.ini";//要删除的文件名称

            FolderBrowserDialog folderpath = new FolderBrowserDialog();
            folderpath.Description = "您选择删除的文件夹";
            folderpath.ShowDialog();
            textBox5.Text = folderpath.SelectedPath;
 
       

        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox5.Text.Length == 0) return;
            try
            {
                string[] rootDirs = Directory.GetDirectories(textBox5.Text); //当前目录的子目录:
                string[] rootFiles = Directory.GetFiles(textBox5.Text);        //当前目录下的文件:
                foreach (string s2 in rootFiles)
                {
                    File.Delete(s2);                      //删除文件                   
                }
                foreach (string s1 in rootDirs)
                {
                    Directory.Delete(s1, true);
                }

                MessageBox.Show("删除成功");
            }
            catch (Exception ex)
            {
              
            }
        }

       }

  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值