C#好压命令行操作类

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ProjectTool
{
    /// <summary>
    /// 好压压缩工具类
    /// </summary>
    class HaoZip
    {
        #region 字段
        CommandLineHelper commandLineHelper = new CommandLineHelper();
        /// <summary>
        /// 压缩命令行,默认为压缩
        /// </summary>
        private string _setCompressionCommand = "a";
        /// <summary>
        /// 压缩文件的格式,默认指定为zip压缩格式
        /// </summary>
        private string _setCompressionFormat = "-tzip";
        /// <summary>
        /// 压缩文件的位置
        /// </summary>
        private string _compressedFilePath;
        /// <summary>
        /// 压缩目标(文件夹或者文件)的路径
        /// </summary>
        private string _compressedTargetPath;
        /// <summary>
        /// 设置工作目录
        /// </summary>
        private string _setWorkingDirectory = "-w";
        /// <summary>
        /// 工作目录路径
        /// </summary>
        private string _workingDirectoryPath = @"D:\";
        #endregion

        #region 方法
        /// <summary>
        /// 得到好压压缩命令行字符串
        /// 将此字符串赋予CommandLineHelper.RunCmd()方法即可执行压缩命令
        /// </summary>
        /// <param name="compressedFilePath">压缩文件的位置</param>
        /// <param name="compressedTargetPath">压缩目标(文件夹或者文件)的路径</param>
        /// <returns></returns>
        public string CompressedZipCommand(string compressedFilePath, string compressedTargetPath)
        {
            string command;//定义变量,存储好压命令行
            System.DateTime currentTime = System.DateTime.Now;//定义变量,存储系统当前时间
            int charZipIndex = compressedFilePath.IndexOf(".zip", 0);//定义变量,获取".zip"在名称中的索引
            _compressedFilePath = compressedFilePath.Insert(charZipIndex, currentTime.ToString("yyyy-MM-dd HH_mm_ss"));//拼接字符串,将系统当前时间插入zip备份文件的名称中
            _compressedTargetPath = compressedTargetPath;
            return command = string.Format(" " + _setCompressionCommand + " " + _setCompressionFormat + " " + '"' + _compressedFilePath + '"' + " " + '"' + _compressedTargetPath + '"' + " " + _setWorkingDirectory + '"' + _workingDirectoryPath + '"');
        }
        #endregion
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;

namespace ProjectTool
{
    /// <summary>
    /// 命令行操作类类
    /// </summary>
    class CommandLineHelper
    {
        Process process = new Process();//初始化 System.Diagnostics.Process 类的新实例
        #region 接收命令并执行它,目前执行haozipc
        /// <summary>
        /// 接收命令并执行它,目前执行haozipc
        /// </summary>
        /// <param name="command">执行的命令</param>
        /// <returns>进程是否执行成功</returns>
        public bool RunCmd(string command)
        {
            bool result = false;//执行结果
            ProcessStartInfo startinfo = new ProcessStartInfo();//初始化 System.Diagnostics.ProcessStartInfo 类的新实例
            startinfo.FileName = "haozipc";//指定启动的
            startinfo.Arguments = command; //设置命令参数
            startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏窗口
            process = new Process();//初始化 System.Diagnostics.Process 类的新实例。
            process.StartInfo = startinfo;//获取或设置要传递给 System.Diagnostics.Process 的 System.Diagnostics.Process.Start()方法的属性。
            try
            {
                process.Start();//启动进程
                process.WaitForExit(); //无限期等待进程 winrar.exe 退出
                if (process.HasExited)//获取指示关联进程是否已终止的值。
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                process.Close();//释放与此组件关联的所有资源
            }
            return result;//返回进程执行结果
        }
        #endregion
    }
}


转载于:https://my.oschina.net/dongri/blog/610906

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值