C# 调用好压

    public class HaoZip
    {
        static  string  RunCmd(string command)
        {
            // string exePath = Directory.GetCurrentDirectory()+ @"\haoya";// 复制好压的文件到exe下,haoya文件夹内
            string exePath = @"C:\Program Files\2345Soft\HaoZip";//调用安装好的

            Process p = new Process();
            //设置要启动的应用程序
            p.StartInfo.FileName = "cmd";
            //是否使用操作系统shell启动
            p.StartInfo.UseShellExecute = false;
            // 接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardInput = true;
            //输出信息
            p.StartInfo.RedirectStandardOutput = true;
            // 输出错误
            p.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p.StartInfo.CreateNoWindow = true;
            //启动程序
            p.Start();
            //向cmd窗口发送输入信息
            p.StandardInput.WriteLine("cd " + exePath);
            p.StandardInput.WriteLine("cls");
            p.StandardInput.WriteLine(command+ "&exit");
            p.StandardInput.AutoFlush = true;
            //获取输出信息
            string strOuput = p.StandardOutput.ReadToEnd();
            //等待程序执行完退出进程
            p.WaitForExit();
            //释放与此组件关联的所有资源
            p.Close();
            Console.WriteLine(strOuput);
            return strOuput;
        }

        public static void  JianYa(string file_zip = @"D:\1.zip", string folderPath = @"D:\")
        {
            string cmdStr = "";
            cmdStr += "HaoZipC.exe";
            cmdStr += " x ";
            cmdStr += file_zip;
            cmdStr += " -o";
            cmdStr += folderPath;
            RunCmd(cmdStr);
        }

        public static void CreatZip(string file_path = @"D:\1.txt",string zipFile = @"D:\1.zip")
        {
            string cmdStr = "";
            cmdStr += "HaoZipC.exe";
            cmdStr += " a -tzip ";
            cmdStr += zipFile;
            cmdStr += " ";
            cmdStr += file_path;
            cmdStr += " -r";
            RunCmd(cmdStr);
        }

        public static int GetfileNum(string file_zip = @"D:\1.zip")
        {
            string cmdStr = "";
            cmdStr += "HaoZipC.exe";
            cmdStr += " l ";
            cmdStr += file_zip;
            var strOuput = RunCmd(cmdStr);

            var fileNum = 0;
            var index = strOuput.IndexOf(" 个文件");
            if(index==-1)
            {

            }else
            {
                fileNum =int.Parse(strOuput.Substring(index - 2, 2));
            }

            return fileNum;
        }

        public static int GetfolderNum(string file_zip = @"D:\1.zip")
        {
            string cmdStr = "";
            cmdStr += "HaoZipC.exe";
            cmdStr += " l ";
            cmdStr += file_zip;
            var strOuput= RunCmd(cmdStr);

            var folderNum =0;
            var arr = strOuput.Split(new string[] { "\r\n" }, System.StringSplitOptions.None);
            if(arr.Length>=24)
            {
                var LinefileNum = arr[arr.Length - 2];
                var strArr = LinefileNum.Split(' ');
                if(strArr.Length>=53)
                {
                    folderNum = int.Parse(strArr[52]);
                }
            }
            return folderNum;
        }

        public static string GetFirstFileName(string file_zip = @"D:\1.zip")
        {
            string cmdStr = "";
            cmdStr += "HaoZipC.exe";
            cmdStr += " l ";
            cmdStr += file_zip;
            var strOuput = RunCmd(cmdStr);

            var firstFileName = "";
            var arr = strOuput.Split(new string[] { "\r\n" }, System.StringSplitOptions.None);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(i + "  " + arr[i]);
            }

            if (arr.Length >= 24)
            {
                var Linefile = arr[21];
                var fileListArr = Linefile.Split(' ');
                if (fileListArr.Length >= 27)
                {
                    firstFileName = fileListArr[27];
                }
            }
            return firstFileName;
        }

    }

 

你可能感兴趣的:(C#)