Exemple #1
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            string           path   = txtCombinePath.Text.Trim();
            var dirList             = new List <string>();

            #region 处理层级结构
            if (level == 2)
            {
                var dirInfo = new DirectoryInfo(path);
                dirList.Add(dirInfo.FullName);
            }
            if (level == 3)
            {
                var dirInfo = new DirectoryInfo(path);
                var dirii   = dirInfo.GetDirectories();
                foreach (DirectoryInfo ii in dirii)
                {
                    dirList.Add(ii.FullName);
                }
            }
            #endregion

            string currentFilePath = "";

            try
            {
                foreach (var str in dirList)
                {
                    #region 发包方处理

                    string          fbfPath = Path.Combine(str, "发包方");
                    DirectoryInfo   dir     = new DirectoryInfo(fbfPath);
                    DirectoryInfo[] dirArr  = dir.GetDirectories();
                    foreach (DirectoryInfo dirItem in dirArr)
                    {
                        try
                        {
                            string dirName = dirItem.Name;
                            currentFilePath = dirItem.FullName;
                            var addFlag = CombineToPDF.CheckPdfJpg(dirItem);
                            CombineToPDF.dirPhoto2Pdf(dirItem, addFlag);         //处理图片转PDF
                            string[] fileArr = CombineToPDF.getFileArr(dirItem); //获取PDF文件

                            if (fileArr.Length == 0)
                            {
                                continue;
                            }

                            string pdfN       = dirName.Split('#')[1].Trim();
                            string newPdfName = Path.Combine(fbfPath, pdfN + ".pdf");
                            PdfUtility.MergePDF(fileArr, newPdfName);

                            int fileCount = dir.GetFiles().Length;   //获取文件夹下是否有文件,如果没有任何文件则不进行删除
                            if (ckIsDelete.Checked && fileCount > 0) //文件夹下如果没有任何文件则不进行删除
                            {
                                Directory.Delete(Path.Combine(fbfPath, dirName), true);
                            }
                            currentpdfIndex += fileArr.Length;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("【" + dirItem.FullName + "】路径下文件命名格式有误,请检查!");
                            throw ex;
                        }

                        int progeress = currentpdfIndex * 100 / processTotalCount;
                        worker.ReportProgress(progeress);
                        if (progeress == 100) // 如果用户取消则跳出处理数据代码
                        {
                            e.Cancel = true;
                            ShowCompletePath();
                            break;
                        }
                    }

                    #endregion

                    #region 承包方处理

                    string          cbfPath   = Path.Combine(str, "承包方");
                    DirectoryInfo   cbfDir    = new DirectoryInfo(cbfPath);
                    DirectoryInfo[] cbfDirArr = cbfDir.GetDirectories();
                    foreach (DirectoryInfo dirItem in cbfDirArr)
                    {
                        string          dirItemName = dirItem.Name;
                        DirectoryInfo[] cbfDirArr2  = dirItem.GetDirectories();
                        foreach (DirectoryInfo item in cbfDirArr2)
                        {
                            string dirName = item.Name;
                            currentFilePath = item.FullName;
                            var addFlag = CombineToPDF.CheckPdfJpg(dirItem);
                            CombineToPDF.dirPhoto2Pdf(item, addFlag); //处理图片转PDF
                            string[] fileArr = CombineToPDF.getFileArr(item);
                            if (fileArr.Length == 0)
                            {
                                continue;
                            }

                            string pdfN = dirName.Split('#')[1].Trim();
                            //pdfN = EncryptUtil.UnAesStr(pdfN, keyVal, ivVal);
                            string newPdfName = Path.Combine(cbfPath, pdfN + ".pdf");
                            PdfUtility.MergePDF(fileArr, newPdfName);

                            int fileCount2 = item.GetFiles().Length;  //获取文件夹下是否有文件,如果没有任何文件则不进行删除
                            if (ckIsDelete.Checked && fileCount2 > 0) //文件夹下如果没有任何文件则不进行删除
                            {
                                Directory.Delete(item.FullName, true);
                            }
                            currentpdfIndex += fileArr.Length;

                            int progeress = currentpdfIndex * 100 / processTotalCount;
                            worker.ReportProgress(progeress);
                            if (progeress == 100) // 如果用户取消则跳出处理数据代码
                            {
                                e.Cancel = true;
                                ShowCompletePath();
                                break;
                            }
                        }
                        int fileCount = dirItem.GetDirectories().Length; //获取文件夹下是否有文件夹,如果没有任何文件夹则进行删除
                        if (ckIsDelete.Checked && fileCount == 0)        //文件夹下如果没有任何文件则不进行删除
                        {
                            Directory.Delete(Path.Combine(cbfPath, dirItemName), true);
                        }
                    }

                    #endregion
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(currentFilePath + "文件夹下文件有误,请检查!\r\n错误信息:" + ex.Message);
                return;
            }
        }