Example #1
0
        /// <summary>
        /// Validate files after convert and delete lost ones
        /// </summary>
        /// <param name="listOfFiles"></param>
        /// <param name="extension"></param>
        public static void PosValidateExpectedFiles(List <string> listOfFiles, string extension = "xlsb")
        {
            FileWorker    fw            = new FileWorker();
            List <string> expectedFiles = new List <string>();

            foreach (string filePath in listOfFiles)
            {
                string fileWithoutExt = Helper.SplitExstansionByDotAndTakeName(filePath);
                expectedFiles.Add(fileWithoutExt + "." + extension);
            }

            int isExistsCount = 0;

            foreach (string newFilePath in expectedFiles)
            {
                if (File.Exists(newFilePath))
                {
                    isExistsCount++;
                }
            }
            if (isExistsCount == listOfFiles.Count)
            {
                foreach (string oldFilePath in listOfFiles)
                {
                    if (File.Exists(oldFilePath))
                    {
                        try
                        {
                            fw.DeleteFiles(oldFilePath);
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Resave in correct extension and return true if file was saved successfully
        /// </summary>
        public bool ResaveExcelFile(string folderPath, string filePath, string newFileName, byte extension, string oldName)
        {
            bool flag = false;

            currentTryCount = 0;
            FileWorker fileWorker = new FileWorker();

            bool retry       = true;
            byte maxTryCount = 20;

            while (retry && maxTryCount > currentTryCount)
            {
                try
                {
                    xlApp = new Microsoft.Office.Interop.Excel.Application();
                    if (hWnd == 0)
                    {
                        try
                        {
                            hWnd = xlApp.Hwnd;
                        }
                        catch (Exception ex)
                        {
                            errorsMessages.Add(ex.Message);
                            hWnd = 0;
                        }
                    }
                    retry           = false;
                    currentExeption = null;
                }
                catch (Exception ex)
                {
                    currentTryCount++;
                    currentExeption = ex;
                    errorsMessages.Add(ex.Message);
                    if (hWnd == 0)
                    {
                        try
                        {
                            hWnd = xlApp.Hwnd;
                        }
                        catch (Exception ex1)
                        {
                            errorsMessages.Add(ex1.Message);
                            hWnd = 0;
                        }
                    }
                    lastException = ex;
                }
            }

            currentTryCount = 0;
            retry           = true;

            if (currentExeption == null)
            {
                xlApp.Visible       = false;
                xlApp.DisplayAlerts = false;
                xlApp.UserControl   = false;

                while (retry && maxTryCount > currentTryCount)
                {
                    try
                    {
                        xlWorkbook      = xlApp.Workbooks.Open(filePath, 0, false, 5, "", "", true, XlPlatform.xlWindows, "", false, false, 0, false, 1, 0);
                        retry           = false;
                        currentExeption = null;
                    }
                    catch (Exception ex)
                    {
                        errorsMessages.Add(ex.Message);
                        currentTryCount++;
                        currentExeption = ex;
                        lastException   = ex;
                    }
                }

                currentTryCount = 0;
                retry           = true;

                if (currentExeption == null)
                {
                    newFileName = Helper.SplitExstansionByDotAndTakeName(newFileName);
                    Microsoft.Office.Interop.Excel.XlFileFormat excelExstansion = PickExstansion(extension);
                    string oldExtensionInString = Helper.GetExtension(filePath);
                    string fullFilePathAndName  = folderPath + @"\" + newFileName;

                    while (retry && maxTryCount > currentTryCount)
                    {
                        try
                        {
                            xlWorkbook.SaveAs(fullFilePathAndName, excelExstansion, Type.Missing, Type.Missing,
                                              false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
                                              Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                            retry           = false;
                            currentExeption = null;
                        }
                        catch (Exception ex)
                        {
                            errorsMessages.Add(ex.Message);
                            currentTryCount++;
                            currentExeption = ex;
                            lastException   = ex;
                        }
                    }

                    currentTryCount = 0;
                    retry           = true;

                    if (currentExeption == null)
                    {
                        DisposeCOMObjects();
                        if (IsCurrentExtensionCoincidence(oldExtensionInString, excelExstansion) && Helper.IsStringContains(filePath, newFileName))
                        {
                            flag = true;
                        }
                    }
                    else
                    {
                        if (hWnd == 0)
                        {
                            try
                            {
                                hWnd = xlApp.Hwnd;
                            }
                            catch (Exception ex)
                            {
                                hWnd = 0;
                            }
                        }
                        DisposeCOMObjects();
                        return(flag);
                    }
                }
                else
                {
                    if (hWnd == 0)
                    {
                        try
                        {
                            hWnd = xlApp.Hwnd;
                        }
                        catch (Exception ex)
                        {
                            errorsMessages.Add(ex.Message);
                            hWnd = 0;
                        }
                    }
                    DisposeCOMObjects();
                    return(flag);
                }
            }
            else
            {
                if (hWnd == 0)
                {
                    try
                    {
                        hWnd = xlApp.Hwnd;
                    }
                    catch (Exception ex)
                    {
                        errorsMessages.Add(ex.Message);
                        hWnd = 0;
                    }
                }
                DisposeCOMObjects();
                return(flag);
            }
            if (hWnd == 0)
            {
                try
                {
                    hWnd = xlApp.Hwnd;
                }
                catch (Exception ex)
                {
                    errorsMessages.Add(ex.Message);
                    hWnd = 0;
                }
            }
            DisposeCOMObjects();

            return(flag);
        }
        /// <summary>
        /// Resave files with old name, changing it to new name
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        public void ResaveFilesInFolder(string[] args)
        {
            Helper.ExstractDataFromArgs(args, out folderPath, out oldName, out newName, out extension);
            PlusReplaceCompleted();
            byte       excelExtension = Helper.ExtensionToByte(extension);
            FileWorker fileWorker     = new FileWorker();

            fileWorker.Logs("", "1");
            List <string> fullPath = new List <string>();

            fullPath = fileWorker.GetAllFullFilePath(fileWorker.SortFilesByNameAndExt(fileWorker.GetAllFiesInDirectory(folderPath), oldName));

            List <string> fileNames = new List <string>();

            fileNames = fileWorker.GetAllFileNames(fileWorker.SortFilesByNameAndExt(fileWorker.GetAllFiesInDirectory(folderPath), oldName));

            List <string> filesForDeletion = new List <string>();
            List <string> sameFiles        = new List <string>();

            if (fullPath.Count != 0)
            {
                int  q    = 0;
                bool flag = false;

                foreach (string s in fullPath)
                {
                    fileWorker.Logs(s);
                    ExcelWorker excelWorker = new ExcelWorker();
                    try
                    {
                        if (!excelWorker.ResaveExcelFile(folderPath, s, Helper.Replace(fileNames[q], oldName, newName), excelExtension, oldName))
                        {
                            q++;
                            flag = true;
                            filesForDeletion.Add(s);
                            excelWorker.GetLastErrors(ref errorsMessages);
                            if (errorsMessages.Count != 0)
                            {
                                fileWorker.Logs(errorsMessages);
                            }
                            GColectorStart();
                        }
                        else
                        {
                            excelWorker.GetLastErrors(ref errorsMessages);
                            if (errorsMessages.Count != 0)
                            {
                                fileWorker.Logs(errorsMessages);
                            }
                            GColectorStart();
                        }

                        GColectorStart();
                    }
                    catch (Exception ex)
                    {
                        level2Exception = ex;
                        fileWorker.Logs("Level 2 Error" + level2Exception.Message);
                        GColectorStart();
                    }
                    if (flag != true)
                    {
                        q++;
                        GColectorStart();
                    }
                    flag = false;
                    GColectorStart();
                }

                DeleteFiles(filesForDeletion);
                fileWorker.Logs(statusString);
                fileWorker.Logs("\n");
            }
        }
Example #4
0
        /// <summary>
        /// Dispose COM objects
        /// </summary>
        private void DisposeCOMObjects()
        {
            FileWorker fileWorker = new FileWorker();

            try
            {
                xlWorkbook.Close(false);
            }
            catch (Exception ex)
            {
                errorsMessages.Add(ex.Message);
            }

            try
            {
                xlApp.Quit();
            }
            catch (Exception ex)
            {
                errorsMessages.Add(ex.Message);
                if (ex.Message == "Exception from HRESULT: 0x800AC472")
                {
                    GetWindowThreadProcessId((IntPtr)hWnd, out pid);
                    if (hWnd != 0)
                    {
                        try
                        {
                            Process[] procs = Process.GetProcessesByName("EXCEL");
                            if (pid != 0)
                            {
                                foreach (Process p in procs)
                                {
                                    if (p.Id == pid)
                                    {
                                        fileWorker.Logs(p.Id.ToString());
                                        p.Kill();
                                    }
                                }
                            }
                        }
                        catch (Exception ex1)
                        {
                            errorsMessages.Add(ex1.Message);
                        }
                    }
                }
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();

            try
            {
                Marshal.ReleaseComObject(xlWorkbook);
            }
            catch (Exception ex)
            {
                errorsMessages.Add(ex.Message);
            }
            try
            {
                Marshal.ReleaseComObject(xlApp);
            }
            catch (Exception ex)
            {
                errorsMessages.Add(ex.Message);
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }