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.