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. WayneAKing 4,931 Reputation points
    2022-11-16T20:25:24.36+00:00

    Drawing comparisons between what is allowed without interventions
    between Windows File Explorer or cmd shell commands, etc. and
    programs that you have just created are not meaningful much
    of the time. The former are part of the OS itself or closely
    related and are well-known to Windows and security applications.
    By contrast every time you do a Build you are creating an entirely
    new and unknown - and unsigned - application.

    These are often subject to more rigorous scrutiny by security
    software and the OS itself. For example, Windows Smartscreen
    will treat your program with suspicion and UAC will signal
    that it is unknown and unsigned by the colour of the UAC
    dialog.

    Using VS 2017, with your first test I only get the message
    you posted IF the file 01.jpg has the Read-Only attribute set.
    If it isn't set, the program executes as expected.

    A Read-Only attribute on the source file will also be set
    on the destination file. So after a copy of a Read-Only
    file the source file cannot be overwritten or deleted and
    the destination (interim) file cannot be deleted.

    Stepping through the code and checking the directory
    with File Explorer the files appear as expected after
    each step.

    One small addition may be helpful in cases where the
    program is interrupted/aborted before completion:

    'My.Computer.FileSystem.CopyFile(strPath & strFileName,   
    'strPath & strNewFileName)  
      
    My.Computer.FileSystem.CopyFile(strPath & strFileName,   
    strPath & strNewFileName, True)  
      
    

    This will alow overwriting the Copy-To file if it exists.

    • Wayne
    1 person found this answer helpful.
    0 comments No comments

  2. MotoX80 36,291 Reputation points
    2022-11-14T23:44:51.887+00:00

    What are the NTFS permissions on the G:\Temp folder? Verify that your account (or groups like "everyone" or "users") has full control on the folder. Then check the permissions on the file. The check marks should be a light grey which indicates that they are inherited, Like this.

    260292-image.png


  3. NL40 6 Reputation points
    2022-11-15T13:13:50.447+00:00

    Just to eliminate the possibility of security settings on my system from being the issue I performed the following test. I went ahead and Disabled inheritance on my G:\_Temp directory, then replaced all child object permissions and manually set the permissions for my 01.jpg file to give full access to Users, which is all users on the system. Still when I run the program I get two prompts to delete the file, one for admin permission and one for unknown publisher.
    Next from the windows command prompt I changed directory to C:\Users\%username%\Documents, which puts me in my user ID's document directory. Initially I copied the 01.jpg file to that directory. I updated my program to set strPath = "C:\Users\%username%\Documents" where %username% was my actual user ID and stepped through it. The program copied the file to the new name and when it came time to delete it I still get two prompts, one for admin permission and one for unknown publisher. Since the file was copied from the G:\ drive I thought that it might have carried it's permissions with it so I did another test. This time at the command prompt I ran this command: echo Testing >09.txt which created a new file containing the word Testing. I then updated my program to operate on this file:

    Dim strPath = "C:\Users\%username\Documents\"
    Dim strFileName As String = "09.txt"
    Dim strNewFileName As String = strFileName & ".XYZ.txt"

    The result is exactly the same, two prompts, one for admin permission and one for unknown publisher.

    0 comments No comments

  4. NL40 6 Reputation points
    2022-11-15T15:54:15.87+00:00

    With my latest test I have absolutely no doubt that this is a Visual Basic problem, not a permission issue. I downloaded and installed Exiftool. Using this tool I'm able to update the JPG file. By default Exiftool updates the file and writes the result to a temporary file. However, based on their documentation if you use the -overwrite_original command line argument it will "Overwrite original by renaming tmp file". I just used Exiftool to update my 01.jpg file and I got no prompts for administrator permissions. Is there any other way to delete a file in VB.net? I've tried My.Computer.FileSystem.DeleteFile, IO.File.Delete and kill. My.Computer.FileSystem.DeleteFile works if I use the additional arguments, but I get two prompts, one for admin permission and one for unknown publisher. If I use it without all the argument it behaves the save as IO.File.Delete and kill. I just get "Access to the path 'G:_Temp\01.jpg' is denied."


  5. NL40 6 Reputation points
    2022-11-16T18:14:34.553+00:00

    No, G drive is just another partition on one of my internal drives. Nothing special about it. I do not get any errors creating a file using echo. I'm actually convinced the problem is with VS being corrupt somehow. I tried to un-install it, but it would not even uninstall. So I used an uninstall utility to remove the files and clean up the registry as much as it could. I then re-installed VS 2019, but it does the same thing. I don't think I will have time to work on this until tomorrow afternoon, but by next steps are to try to un-install VS 2019 and install VS 2022 if I can. If that doesn't work I have the ability to multi boot on my workstation, so I can setup a separate partition, with no other drive, do a clean install of Win 10 from scratch, install VS 2019 and do the test on my C:\Users\%UserName%\Documents directory. If that's successful I can then add the G partition and try the code on _Temp directory. Hopefully I can get this done by Friday or Saturday and report back the results.


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.