1

I have the strangest of issues.

I want to do the following, in a Windows console app, C#, .Net 4.5.2, VS2017 (15.7.3).

foreach (var f in targetFiles)
{
    File.Delete(f);
}

targetFiles is a List<string> with a full path spec eg C:\Directory\Filename.ext. My path strings are correctly constructed, so double \\ as required. My target directory is C:\Program Files\Provider Name\AppName\ and it has .dll, .png, .xml and a few other file types in it.

The above code gives an UnauthorizedAccessException on the .png files only. If I change the extension of the .png files to (say) .tmp, I don't get an exception.

The files don't have any special attributes, although I've tried using File.SetAttributes(filename, FileAttributes.Normal), which made no difference.

I've exhaustively gone through the permissions on the folder, including giving Everyone full access, propagating permissions to children etc. I'm admin on my own machine, and I've tried executing via VS and the .exe itself, including running as administrator.

I've tried copying the folder in its entirety to C:\ and pointing the app to this location, resulting in the same behaviour.

There is code which impersonates a network user (wider purpose is to facilitate across network updates) - I've switched this on and off with no difference in behaviour.

Does anyone have some pointers to provide me with? I've read lots of related posts but so far have had no success with the principal suggestions (file attributes, permissions, VS permissions). The only thing left on my list at the moment is to see if could be something in the domain administration which is interfering.

Any help would be greatly appreciated.

Sach
  • 10,091
  • 8
  • 47
  • 84
lukep
  • 111
  • 10
  • try to delete the "png" file manually and see what will happen. – Ray Krungkaew Jul 06 '18 at 16:36
  • See [documentation](https://msdn.microsoft.com/en-us/library/system.unauthorizedaccessexception(v=vs.110).aspx) here. The exception should have more information about what's causing the exception, such as whether you have read/write permission etc. – Sach Jul 06 '18 at 16:36
  • I have similar issues like yours sometimes with the VS build system. Sometimes some locks are not released as they should be, but renaming the file works and releases the lock. My guess is, some other process is locking the file. – derpirscher Jul 06 '18 at 16:36
  • 2
    Well, PNG files are indeed special. Programs often try to display the content of the file and that puts a lock on file. Standard example in .NET is the Bitmap class, the lock won't be released until its Dispose() method is called. Might be your own program doing this, if not then consider the SysInternals' Handle utility to find the program that does this. – Hans Passant Jul 06 '18 at 16:41
  • I find Hans Passant's answer appropriate and want to add that my first thoughts after reading this question were AV scans, Explorer / image viewer creating thumbnails or something else. – Rainer Schaack Jul 06 '18 at 16:56
  • If you are loading that image any where in your program, c# puts a lock on it (depending on what your doing). I've found using a System.Windows.Media.Imaging.BitmapImage, then setting the urisource, then calling Freeze() on the bitmap image should unlock the file . That is if you are using in your program. – Gauthier Jul 06 '18 at 16:58
  • Many thanks for all your comments. @RawitasKrungkaew - I can manually delete the file using Windows Explorer without any issue. – lukep Jul 06 '18 at 21:01
  • @Sach, I'll check out your link thanks. – lukep Jul 06 '18 at 21:01
  • @Gauthier - another executable in the solution can load these files, but not the executable I'm using. – lukep Jul 06 '18 at 21:01
  • @Derpirscher et al - locks seem a good place to look next, thank you. – lukep Jul 06 '18 at 21:02

2 Answers2

1

It can be antivirus who blocks your code deleting files. Try to add your folder to antivirus's exception list.

Sergei Zinovyev
  • 1,238
  • 14
  • 14
0

Well, this is an unconfirmed answer as I'll have to speak to the network admin, but it turns out that Sergei Zinovyev put me on the right track (thank you). I looked at the activity in McAfee's anti-virus status monitor and saw a number of occurrences of DOMAIN\USER ran PROGRAM.EXE, which tried to access C:\DIRECTORY, violating the rule "Deleting files commonly targeted by ransomware-class malware", and was blocked. For information about how to respond to this event, see KB85494 (real data substituted).

For reference, I tried Handle and couldn't find any references to my files, so I also tried Eric J.'s answer here which returned no locks on the file.

It seems odd that it only fails with .png though - a bit bizarre that I'm allowed to delete dlls but not pngs.

When I can get the rule relaxed on antivirus I'll respond again to confirm that the problem has been resolved.

Thanks for all your help.

lukep
  • 111
  • 10
  • Just to confirm that this was the problem, my application has been excluded from the rule and the files are now deleting as expected, so I've ticked this as the answer, though the credit of course belongs to Sergei Zinovyev. – lukep Jul 10 '18 at 09:56