首页 > Windows开发 > 详细

C# 用户选择单个压缩-系统自带压缩

时间:2019-03-27 01:08:08      阅读:226      评论:0      收藏:0      [点我收藏+]
            //用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);   
                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 + " 该文件夹不存在"); //如果文件夹不存在则提示 
                }
  1.   

C# 用户选择单个压缩-系统自带压缩

原文:https://www.cnblogs.com/xiongyunsheng/p/10604935.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!