//用C#自带的压缩,最少要.net4.5或以上,先增加引用 System.IO.Compression.FileSystem
// FolderBrowserDialog dlg = new FolderBrowserDialog(); //压缩目录------显示一个标准选择文件夹对话框
OpenFileDialog dlg = new OpenFileDialog(); //显示一个标准选择文件对话框
DialogResult result = dlg.ShowDialog(); //返回对话框的值 选择值
if (result==DialogResult.OK) //如果用户在对话框选择了文件夹或文件
{
string zipPath = System.IO.Path.GetDirectoryName(dlg.FileName); //返回指定的路径
string abc = zipPath + "\\"+System.IO.Path.GetFileNameWithoutExtension(dlg.FileName); //abc=于当前目录+文件名(不包含扩展号)
System.IO.Directory.CreateDirectory(abc); //创建临时文件夹
System.IO.File.Copy(dlg.FileName, abc + "\\" + dlg.SafeFileName); //复制选择的文件
System.IO.Compression.ZipFile.CreateFromDirectory(abc, abc + ".zip"); //压 缩文件到用户选择文件的目录里
DeleteFolder( abc); //删除临时文件夹
System.Diagnostics.Process.Start("explorer.exe", zipPath); // 启动由包含进程启动信息
void DeleteFolder(string dir) //创建删除文件夹和里面的文件的方法
{
if (System.IO.Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach (string d in System.IO.Directory.GetFileSystemEntries(dir))
{
if (System.IO.File.Exists(d))
System.IO.File.Delete(d); //直接删除其中的文件
else
DeleteFolder(d); //递归删除子文件夹
}
System.IO.Directory.Delete(dir); //删除已空文件夹
MessageBox.Show(dir + " 文件夹删除成功");
}
else
MessageBox.Show(dir + " 该文件夹不存在"); //如果文件夹不存在则提示
}
转载于:https://www.cnblogs.com/xiongyunsheng/p/10604935.html