C# App terminates unexpectedly without leaving trace when deleting more that 5 files

Vatech

Member
Joined
Mar 27, 2022
Messages
6
Programming Experience
3-5
I have a folder with about 150 files ( pictures about 150kb each), and i need a fucntion to delete them.
when running the app it terminates unexpectedly.
-I run VS as administrator
-folder and files are not protected and not in use
-i tryed to delete only one and woked correct
-i placed a counter to my loop and i noticed that it works correct till 5 pictures, if i increase the loop, app terminates unexpectedly with no error.
C#:
System.IO.DirectoryInfo di = new DirectoryInfo("C:\\MS_ACCESS_SOFTWARE\\TestPic\\");
            int Fcounter = 0;
            try
            {
                foreach (FileInfo file in di.GetFiles())
                {
                    if (Fcounter < 5)
                    {
                        file.Delete();
                        Fcounter++;
                    }
                }
                foreach (DirectoryInfo dir in di.GetDirectories())
                {
                    dir.Delete(true);
                }
             }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex);
            }
            return "Finished";
The problem may be caused by Access Violation Exception . Any suggestion how could i solve this?
 
Last edited:
Solution
Permissions on files and folders can be individually set. Also files and folders can be locked by running programs and prevention deletion.

Anyway, what is the exception being thrown?

Delete(true); has always worked for me barring:
- permissions not allowed
- files/folders locked
- aggressive antivirus software heuristics
You are only showing us the try part of your code. What are you doing in with the catch? If you are just eating the exception, then thar could be part of why the app just terminates quietly.

Anyway, if you are getting an access violation, show us the details of that access violation. On which lounge first it occur? What is the message in the exception?

As an aside, what antivirus are you using? Some of them have heuristics that will see some behaviors as malicious and just terminate the app to keep it from doing more damage. Deleting a lot of files may look malicious to it.
 
Last edited:
You are only showing us the try part of your code. What are you doing in with the catch? If you are just eating the exception, then thar could be part of why the app just terminates quietly.
i tryed many different methods. For example code parts:

Delete files within folder:
public static void Empty(this System.IO.DirectoryInfo directory)
{
    foreach (System.IO.FileInfo file in directory.GetFiles()) file.Delete();
    foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
}
//-------------------------------------------------------------------------------------

private void ClearFolder(string FolderName)
{
    DirectoryInfo dir = new DirectoryInfo(FolderName);

    foreach (FileInfo fi in dir.GetFiles())
    {
        try
        {
            fi.Delete();
        }
        catch (Exception) { } // Ignore all exceptions
    }

    foreach (DirectoryInfo di in dir.GetDirectories())
    {
        ClearFolder(di.FullName);
        try
        {
            di.Delete();
        }
        catch (Exception) { } // Ignore all exceptions
    }
}

and more others and also tryed on different folder paths. The problem occurs after the 5th loop, so maximum 5 images deleted.
 
If your folders are deep, you will get a stack overflow exception due to the recursion on line 23. Stack overflow exceptions cannot be caught, but Windows should have reported that your app terminated unexpectedly.

Anyway, just replace lines 12-29 with a single line:
dir.Delete(true);. It should take care of recursively deleting files assuming that files/directories are not locked, and that you have the appropriate permissions (because sometimes admin is not enough).

Any which way, just eating exceptions is a bad idea. You should either handle them if you can, otherwise report them and terminate.

Also Pokemon exception handling in a normally a bad idea at the lower levels in your code. You only want to catch the exceptions you can handle. Catching all of them will cause random errors as your program gets into a more and more corrupted state.
 
Last edited:
If your folders are deep, you will get a stack overflow exception due to the recursion on line 23. Stack overflow exceptions cannot be caught, but Windows should have reported that your app terminated unexpectedly.

Anyway, just replace lines 12-29 with a single line:
dir.Delete(true);. It should take care of recursively deleting files assuming that files/directories are not locked, and that you have the appropriate permissions (because sometimes admin is not enough).
Unfortunately it didnt work.
Also, If that was a permissions related problem i could not delete any picture. As i said, i can delete till 5 pictures. If i increase the loop then i get the problem, Could it be memory related? Although picture are less than 150kb each
 
Permissions on files and folders can be individually set. Also files and folders can be locked by running programs and prevention deletion.

Anyway, what is the exception being thrown?

Delete(true); has always worked for me barring:
- permissions not allowed
- files/folders locked
- aggressive antivirus software heuristics
 
Solution
Permissions on files and folders can be individually set. Also files and folders can be locked by running programs and prevention deletion.

Anyway, what is the exception being thrown?

Delete(true); has always worked for me barring:
- permissions not allowed
- files/folders locked
- aggressive antivirus software heuristics
The issue was caused by antivirus. I am using 360 total security, i disabled it and the all pictures deleted. But how do i solve this? Meaning, how can i activate my antivarus and overcome the issue?
 
Could it be memory related? Although picture are less than 150kb
The memory used by the DirectoryInfo and FileInfo is less than 1KB each. The actual file data is not read into memory. Just the OS information about the directory or file is loaded into memory: name, time stamps, attributes, etc.

If you have lots of files and directories, you can load them progressively (instead of all at once) by using EnumerateFiles() and EnumerateDirectories() which will return one at a time.
 
The memory used by the DirectoryInfo and FileInfo is less than 1KB each. The actual file data is not read into memory. Just the OS information about the directory or file is loaded into memory: name, time stamps, attributes, etc.

If you have lots of files and directories, you can load them progressively (instead of all at once) by using EnumerateFiles() and EnumerateDirectories() which will return one at a time.
please check my reply above, the problem was caused by my antivirus, how can i solve this?
 
The issue was caused by antivirus. I am using 360 total security, i disabled it and the all pictures deleted. But how do i solve this? Meaning, how can i activate my antivarus and overcome the issue?
The cheap way: Most antivirus programs have a way of listing exceptions. You can tag your program as safe/trusted. Or you can mark the directory tree as not to be monitored.

The expensive way: most antivirus programs will trust programs which have been AuthentiCode signed. You will need to buy a code signing certificate and code sign your app. Even though the prices of SSL certificates have fallen to dirt cheap, code signing certificates are still at their same high prices, despite the fact that the difference between SSL certificates and code signing certificates is just a few bits flipped one way instead of another. (Of course chemists will say the same about the difference between lead and gold is just a few subatomic particles.)
 
The cheap way: Most antivirus programs have a way of listing exceptions. You can tag your program as safe/trusted. Or you can mark the directory tree as not to be monitored.

The expensive way: most antivirus programs will trust programs which have been AuthentiCode signed. You will need to buy a code signing certificate and code sign your app. Even though the prices of SSL certificates have fallen to dirt cheap, code signing certificates are still at their same high prices, despite the fact that the difference between SSL certificates and code signing certificates is just a few bits flipped one way instead of another. (Of course chemists will say the same about the difference between lead and gold is just a few subatomic particles.)
In this part of cod, should i add somewhere the "Delete(true);"
In any case your answer was very helpful and fixed my issue. Thank you
C#:
System.IO.DirectoryInfo di = new DirectoryInfo("C:\\MS_ACCESS_SOFTWARE\\TestPic\\");
            int Fcounter = 0;
            try
            {
                foreach (FileInfo file in di.GetFiles())
                {
                    
                        file.Delete();
                        Fcounter++;
                        await Task.Delay(10);
                    
                }
                foreach (DirectoryInfo dir in di.GetDirectories())
                {
                    dir.Delete(true);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex);
            }
            return "Finished";
 
Just replace lines 5-16 with di.Delete(true);
 
Back
Top Bottom