C#調用Linux系統命令執行文件合並操作


項目用docker部署在了Linux服務器中,大附件分片上傳后需要進行文件合並,為提高合並速度,使用系統命令進行操作,代碼如下:

      #region 執行Linux系統命令
                try
                {
                    //拼接合並命令中的文件字符串,sourcePath為文件塊所在目錄,targetPath為合並文件的目錄
                    var fileStr = "";
                    for (var i = 0; i < files.Length; i++)
                    {
                        fileStr += fileStr.Length > 0 ? " " + sourcePath + i.ToString() : sourcePath + i.ToString();
                    }

                    //合並文件命令(cat /home/test/1.txt home/test/2.txt >home/test/all.txt)
                    string command = "cat " + fileStr + " >" + targetPath.Replace("\\", "/");
                    //執行結果
                    string result = "";
                    using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
                    {
                        proc.StartInfo.FileName = "/bin/bash";
                        proc.StartInfo.Arguments = "-c \" " + command + " \"";
                        proc.StartInfo.UseShellExecute = false;
                        proc.StartInfo.RedirectStandardOutput = true;
                        proc.StartInfo.RedirectStandardError = true;
                        proc.Start();

                        result += proc.StandardOutput.ReadToEnd();
                        result += proc.StandardError.ReadToEnd();

                        proc.WaitForExit();
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("合並報錯:" + ex.Message);
                }
        #endregion

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM