//写代码前 先在工具箱里 拉几个按钮和 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)
{
}
}
}
}