Access Denied deleting file I own from VB.net

NL40 6 Reputation points
2022-11-14T20:40:37.783+00:00

My apologies for making this post a bit long, but I've done multiple tests and can't figure out where the problem is so I'm hoping someone can help me after seeing all my tests. The issue is that I'm having problem deleting a file that I own and have full access to. Originally I was working on updating a JPG file in VB.net 2019, but kept having problems. The only way I've been able to update a JPG file is to update the file I want, write the updated file to a different file name, ensure that update was successful, delete the old file and rename the newly created file to the old file name. Since I had problems as a test I setup a simple console project in VB.net 2019 and ran the following tests.

  1. In my first test using the code listed below all I did was to copy one file to another. I then try to delete the first file and rename the second file back to the original one. The file copies OK, but when the code goes to delete the original file I get an error message saying "Access to the path 'G:_Temp\01.jpg' is denied." Here's the entire code for my test run: Module Module1
    Sub Main()
    Dim strPath = "G:_Temp"
    Dim strFileName As String = "01.jpg"
    Dim strNewFileName As String = strFileName & ".XYZ.jpg"
    My.Computer.FileSystem.CopyFile(strPath & strFileName, strPath & strNewFileName)
    My.Computer.FileSystem.DeleteFile(strPath & strFileName)
    My.Computer.FileSystem.RenameFile(strPath & strNewFileName, strFileName)
    End Sub
    End Module
  2. In this second test I changed My.Computer.FileSystem.DeleteFile(strPath & strFileName)

to

IO.File.Delete(strFileName)  

When I run it I get the same error message saying "Access to the path 'G:_Temp\01.jpg' is denied."

  1. In this test I changed IO.File.Delete(strFileName)

to

My.Computer.FileSystem.DeleteFile(strPath & strFileName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.DoNothing)  

This time I get a prompt saying "You'll need to provide administrator permission to delete this file." I click "Continue" and then the "User Account Control" pops up asking "Do you want to allow this app from an unknown publisher to make changes to your device" and the app name is my Visual Basic project. Technically I guess this works, but it will be very tedious if I'm updating or copy/delete/renaming multiple files. Also, I'm not even sure why I'm getting prompted since I own the file and the directory and have no problem deleting the file from Windows 10 explorer.

  1. For this test I saved my code, exited VB.net and did a "Run as administrator" on the shortcut. I ran the same exact code with the additional parameters for DeleteFile and when it comes time to delete the original file I get a message saying:
    You need permission to perform this action
    You require permission from ComputerName\User to make changes to this file"

    And of course the "User" listed is my login ID. I have the options to "Try Again" or "Cancel."
  2. Next I changed the code to delete the new file that was just created instead of the old one just to verify that VB has access to the path/file and delete operation works just fine. I don't get prompted for administrator permission, nor do I get any warning about an app from an unknow publisher.
  3. Just in case there was something odd about my file's permission I ran the code step by step, allowed it to copy the file, commented out the delete step, switched over to Windows explorer, manually deleted the original file, switched back to the project and allowed the code to rename the new file back to the old one. I then uncommented the delete file step and ran the code again. It behaves exactly the same as before.

Any ideas why I'm getting this behavior and how to get it to not ask for admin permissions or unknown publisher???

Developer technologies VB
{count} vote

6 answers

Sort by: Most helpful
  1. NL40 6 Reputation points
    2022-11-19T16:46:45.85+00:00

    After a couple of days of testing I found the problem and it's Avast Anti Virus. For those who may be interested there's more detail below. If you don't want to read the whole thing jump to the last paragraph for solution.
    To begin with let me explain my environment. I use BootIT Bare Metal on my system to allow me to multi boot, which means I can install different versions of Windows if I wanted as well as Linux and even at one time I used to run OS2 Warp. I can select which partitions I want available to each boot and take compressed images of my various partitions. For my Windows I typically have at least four partitions, the C drive for Windows, E for applications that do NOT insist on being installed on C, F drive for games and G for my data.
    So once I suspected VS of being corrupt I tried to uninstall/re-install but that didn't work. Next I took a fresh image of my C drive and wiped it. I then installed Windows 10 from scratch without any LAN connection. I installed VS 2019 from DVD (local only, no internet download) and setup a new console project. I created a test text file in my Documents directory. The code was able to copy, delete original and rename the copy back to original without any prompts. Next I installed Comodo Firewall, rebooted and ran the same test successfully. I then rebooted and added my other three partitions. The code ran successfully on G:_Temp\01.jpg. So at this point I could try to reinstall all apps and reconfigure everything from scratch, which would probably take a couple of weeks. Of course, since the problem ended up being Avast I would have found it very quickly as it is always the second app I install right after the FW. But since I didn't know that at the time I decided to restore my previous Windows drive images to see if any of them would work and not have to reinstall everything. I restored an image for my C drive from 6 months earlier and rebooted. When the system came up I noticed that Avast would not start. I went ahead and ran the VS code. It worked properly. I then repaired Avast and rebooted. Everything came up properly. I ran the code and this time it encountered the two prompts, one for admin permission and one for unknown publisher, verifying that Avast was the source of all my problems.
    I then restored my latest image to C drive, disabled Avast Shield Control for 10 minutes, ran the test again and everything worked fine. Enabled all shields and I would get the two prompts. Next I started Avast interface, went to settings, Blocked & Allowed Apps and added the exe file from my project that gets built each time I run the code (in ProjectName\bin\debug). I no longer get the two prompts and everything works perfectly. Thank you everyone for your help and patience. I hope this is useful to anyone else who may be encountering similar issues.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.