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)
            {
              
            }
        }

       }

  
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章