Randomly thrown IO exceptions in C #

I am working on a program for archiving old customer data for the company in which I work. The program copies data from the production server to the local computer, creates a ZIP file of all data and then copies it to the archive server.

After that, it deletes the source files and local copies. From time to time, software errors because they cannot delete local copies from my computer. This is not a mistake for every folder that it closes. I will be mistaken after 300 of them or after 5. It gives one of the following three errors: "The directory is not empty", "The file is being used by another process" or "Access to the file was denied." I tried to set the file attributes to normal using forced garbage collection and terminating the winzip process manually.

I really don't understand why this happens only occasionally. I am an administrator on my computer and he should be able to delete files. I figured something else should use it, but there should be nothing else on my machine other than a program in Visual Studio. Thank.

The following is a cleaning method in which it does not delete files and a method that zips files.

[MethodImplAttribute(MethodImplOptions.NoInlining)]
    static void CleanUp(SqlConnection Connection, string jNumber, DirectoryInfo dir, bool error, string prefix)
    {
        if (!error | (!error & emptyFolder))
        {
            try
            {
                SqlCommand updateJob = new SqlCommand(string.Format("update job set archived = 1 where job = {0}", jNumber), sdiConnection);
                updateJob.ExecuteNonQuery();
            }
            catch
            {
                WriteToLog("SQL Error: " + jNumber, "There was an error changing the archive bit to 1 after the job has been archived");
            }

            try
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch
            {
                WriteToLog("Error cleaning up after processing job", "There was an error garbage collecting.");
            }


            try
            {
                //path of the temporary folder created by the program
                string tempDir = Path.Combine(Path.Combine(System.Environment.CurrentDirectory, "Temp"), jNumber);
                //path of the destination folder
                string destDir = Path.Combine(dir.ToString(), jNumber);
                //SetFileAttributes(tempDir);


                try
                {
                    File.Delete(tempDir + ".zip");
                }
                catch (System.IO.IOException)
                {
                    File.Delete(tempDir + ".zip");
                }
                try
                {
                    Directory.Delete(destDir, true);
                }
                catch (System.IO.IOException)
                {
                    Directory.Delete(destDir, true);
                }
                try
                {
                    Directory.Delete(tempDir, true);
                }
                catch (System.IO.IOException)
                {
                    Directory.Delete(tempDir, true);
                }

            }
            catch
            {

                WriteToLog("File Error: " + jNumber, "There was an error removing files and/or folders after the job has been archived. Please check the source server and destination server to make sure everything copied correctly. The archive bit for this job was set.");
                Directory.Delete(Path.Combine(System.Environment.CurrentDirectory, "Temp"), true);
                Directory.CreateDirectory(Path.Combine(System.Environment.CurrentDirectory, "Temp"));
            }
        }


static bool ZipJobFolder(string jNumber, string jPath)
    {
        try
        {
            string CommandStr = @"L:\ZipFiles\winzip32.exe";
            string parameters = "-min -a -r \"" + jNumber + "\" \"" + jPath + "\"";

            ProcessStartInfo starter = new ProcessStartInfo(CommandStr, parameters);
            starter.CreateNoWindow = true;
            starter.RedirectStandardOutput = false;
            starter.UseShellExecute = false;

            Process process = new Process();
            process.StartInfo = starter;
            Console.WriteLine("Creating .zip file");
            process.Start();
            process.WaitForExit();

            Process[] processes;
            string procName = "winzip32.exe";
            processes = Process.GetProcessesByName(procName);
            foreach (Process proc in processes)
            {
                Console.WriteLine("Closing WinZip Process...");
                proc.Kill();
            }

        }
        catch
        {
            WriteToLog(jNumber, "There was error zipping the files of this job");
            return false;
        }
        return true;
    }
+3
source share
4 answers

I noticed this behavior using Windows Explorer, deleting large folders with lots of files and subfolders. But after waiting a bit and then uninstalling again, it works fine.

Because of this, I always thought that it was the disgusting behavior of the operating system.

Although this is not a solution, you can try it by making your application sleep for a while before trying to delete these files and see if an error still occurs.

If the errors go away, this will be due to some time issues. However, I would like to know the source of the problem.

. , , , , , . , , , , .

, .

Edit: , , .

Edit2: , .Net

+1

, - . , AV . , WinZip .

: 1) . 2) , , WinZip ZipStorer (http://zipstorer.codeplex.com/). , , . , .

+1

, , File.Move File.Copy. , , . , , .

0

Antivirus software can be a problem, because if antivirus software is currently reading your file, it will lead to the fact that, to be honest, I saw it pop up many times when using the .NET framework, and I just drop the processing in and try to perform any operation on the file until it throws an exception. This also happens if the file is currently being copied or registered in the kernel buffer, if any observer is implemented.

0
source

Source: https://habr.com/ru/post/1756631/


All Articles