Results 1 to 23 of 23

Thread: [RESOLVED] Access Denied deleting file I own in VB.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Resolved [RESOLVED] Access Denied deleting file I own in VB.net

    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 so 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:
    Code:
    Module Module1
        Sub Main()
            Dim strPath = "G:\_Temp\"
            Dim strFileName As String = "01.jpg"
            Dim strNewFileName As String = strFileName & ".XYZ.jpg"
            If (Not File.Exists(strPath & strNewFileName)) Then
                My.Computer.FileSystem.CopyFile(strPath & strFileName, strPath & strNewFileName)
            End If
            If (File.Exists(strPath & strFileName)) Then
                My.Computer.FileSystem.DeleteFile(strPath & strFileName)
                My.Computer.FileSystem.RenameFile(strPath & strNewFileName, strFileName)
            End If
        End Sub
    End Module
    2. In this second test I changed
    Code:
    My.Computer.FileSystem.DeleteFile(strPath & strFileName)
    to
    Code:
    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."

    3. In this test I changed
    Code:
    IO.File.Delete(strFileName)
    to
    Code:
    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.

    4. 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."

    5. 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.

    6. 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???

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,194

    Re: Access Denied deleting file I own in VB.net

    Before I dig into your actual issue, what are you trying to achieve? Looking at your business logic, it seems circular:
    1. If the new file doesn't exist, create a copy of the old file with the new name
    2. If the old file exists, delete the old file and rename the new file with the old name


    It seems like you're doing a whole lot of nothing because you would ultimately wind up with the same thing you started with. What am I missing here?
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    So the code I listed is just a test. My original issue is related to updating a JPG file. I have not found a way to update a JPG file in place. The only way I've been able to do is to update the JPG file I want, save the updated info 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 for my testing I had issue and was running the code step by step, jumping between VS and Windows explorer and manually deleting/renaming files as I was working the issue I got tired of getting errors periodically about source or destination file not being there so I just simply put a couple of if statements in there. In my actual program I have exception handling in place, but to keep the test program really simple I omitted them. In reality my test program started out being as simple as:
    Code:
    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
    Last edited by nl40; Nov 14th, 2022 at 03:45 PM.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,194

    Re: Access Denied deleting file I own in VB.net

    Can you tell me if this works on a test image or if you're still getting a permission error?
    Code:
    Dim directoryTempPath = "G:\_Temp\"
    Dim imageFilename = "01.jpg"
    Dim imagePath = Path.Combine(directoryTempPath, imageFilename)
    
    Using imageInMemory = Image.FromFile(imagePath)
        Using imageInMemoryGraphics = Graphics.FromImage(imageInMemory)
            Using br = New SolidBrush(Color.Black)
                imageInMemoryGraphics.DrawString("Test", New Font("Arial", 20), br, 0, 0)
                imageInMemoryGraphics.Save()
            End Using
        End Using
        imageInMemory.Save(imagePath)
    End Using
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    When it hits the imageInMemory.Save(imagePath) I get this error: A generic error occurred in GDI+.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,194

    Re: Access Denied deleting file I own in VB.net

    I could be steering you down the wrong path, but I think ultimately this is a simple permissions error.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    Thank you for your help, but I don't think so. If it was a permission issue then why I can't delete the file when I run the program as administrator and I get an error message saying it needs permission from my personal ID? I also thought there may be some issues with the directory or file permissions. However, I am able to write to that directory to a new file and delete that same file though the program. I also wrote to a new file through the program, deleted the old file manually and allowed the program to rename the new file to the old one so it would keep whatever permissions it had. But then very next run of the program would have the same issues as before.
    Last edited by nl40; Nov 14th, 2022 at 04:44 PM.

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    I did couple of more tests to try to rule out the possible permission issue. 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 decided 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:
    Code:
    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.

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    Couple of other pieces of information. My personal ID owns drive G:\ and has full permissions to it. All sub directories and files inherit their permissions from the root (G:\). I have verified this several times. I believe this is also why when I run the program as administrator it cannot delete the files and says:
    "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 only get the options to "Try Again" or "Cancel."

    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.

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    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."

  11. #11
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,289

    Re: Access Denied deleting file I own in VB.net

    I think the hyper-focus on folder/file level permissions has been a dead end.

    To me, the "Unknown Publisher" warning you are getting points the finger at something like Windows Defender or some other potentially installed "protective program" trying to be helpful. It sees this unknown program of yours being ran, and that this unknown program is immediately trying to delete files. It is logical that it would attempt to intervene. Exiftool is likely a (signed) known quantity, so it is allowed to perform the same actions.

    If you can whitelist your executable in Windows Defender or whatever other AV/malware protection you may be using, you might find that these issues disappear. That's the direction I would be going down were I in your shoes.

    Good luck.

  12. #12
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,714

    Re: Access Denied deleting file I own in VB.net

    Maybe try going to the User Control Access panel and move the slider bar all the way to the bottom.
    Please remember next time...elections matter!

  13. #13

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    Thank you for your help. I could not whitelist my executable in Windows Defender as "Manage Settings" does not show up. I think that's because I use Avast Antivirus and Comodo Firewall. Comodo already had my executable listed. I added exception for my executable in Avast, but nothing changed. Next, I temporarily disabled UAC, just for testing. I still get the first prompt for admin permissions required, but no longer get the second one for unknown publisher. Of course, after the testing I re-enabled the UAC.

  14. #14
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,714

    Re: Access Denied deleting file I own in VB.net

    Maybe check the permissions on the folder:

    https://windowsreport.com/administra...delete-folder/[HTML]HTML]

    I think you can give access to "Everyone"
    Please remember next time...elections matter!

  15. #15

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    Well I was ready to upload an image that would prove permission is not a problem, but I guess I'm cursed. I keep getting an error message saying "You do not have permission to perform this action. Please refresh the page and login before trying again."
    Anyway, I don't think permission is the issue. As you can see from below I'm able to login with an ID name appropriately NotAdmin, which is a normal user and even though my ID owns the directory and file the NotAdmin ID is able to delete the 01.jpg file. You can see from the query user command that the logged in ID (Active) is NotAdmin and at the bottom you can see the Local Group Membership for this ID is *Users not *Administrators. Note that I also was able to save the screen shot and text capture of the commands to the G:\_Temp directory while logged in as NotAdmin:
    Code:
    C:\Users\NotAdmin>echo %username%
    NotAdmin
    
    C:\Users\NotAdmin>g:
    
    G:\>cd _Temp
    
    G:\_Temp>dir 01.jpg
     Volume in drive G is Data
     Volume Serial Number is 9629-58F5
    
     Directory of G:\_Temp
    
    11/15/2022  09:33 AM            50,953 01.jpg
                   1 File(s)         50,953 bytes
                   0 Dir(s)  19,708,694,528 bytes free
    
    G:\_Temp>del 01.jpg
    
    G:\_Temp>dir 01.jpg
     Volume in drive G is Data
     Volume Serial Number is 9629-58F5
    
     Directory of G:\_Temp
    
    File Not Found
    
    G:\_Temp>query user
     USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
     xxxxxxx                                   1  Disc            2  11/15/2022 10:53 AM
    >notadmin              console             3  Active      none   11/15/2022 1:32 PM
    
    G:\_Temp>net user notadmin
    User name                    NotAdmin
    Full Name
    Comment
    User's comment
    Country/region code          000 (System Default)
    Account active               Yes
    Account expires              Never
    
    Password last set            ‎11/‎9/‎2019 12:19:23 AM
    Password expires             Never
    Password changeable          ‎11/‎9/‎2019 12:19:23 AM
    Password required            No
    User may change password     Yes
    
    Workstations allowed         All
    Logon script
    User profile
    Home directory
    Last logon                   ‎11/‎15/‎2022 1:32:16 PM
    
    Logon hours allowed          All
    
    Local Group Memberships      *Users
    Global Group memberships     *None
    The command completed successfully.
    
    
    G:\_Temp>
    Travel as much as you can, as often as you can and as far as you can

  16. #16

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    Here's that attachment:

    Attachment 186236
    Travel as much as you can, as often as you can and as far as you can

  17. #17
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,289

    Re: Access Denied deleting file I own in VB.net

    Is there anything unique or different about what your G:\ drive is? Is it just another partition on your hard drive, or is it some special drive connected to some sort of online cloud storage service?

    For the sake of "why not try something", what happens if you create a new folder called something like "VBTest" under:

    C:\Users\(YourUserName)\Documents\
    (assuming your system root drive is the C: drive)

    And then change your code so that all paths point to files under "C:\Users\(YourUserName)\Documents\VBTest"?

    Those results may clarify things.

  18. #18

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    Thank you for your attention, but nothing special about G drive, just another partition. As for the documents directory, I've already done that. You can see the details in post #8.
    Travel as much as you can, as often as you can and as far as you can

  19. #19
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,289

    Re: Access Denied deleting file I own in VB.net

    Quote Originally Posted by nl40 View Post
    Thank you for your attention, but nothing special about G drive, just another partition. As for the documents directory, I've already done that. You can see the details in post #8.
    Argh, yes, and I had read that yesterday and forgotten about it.

    Other things I would try if I were in your shoes:

    Create a simple C# project that does the exact same thing, compile it, run it, and see if the same behavior results. My assumption is the same thing will happen.

    Create a simple project in a non Visual Studio.NET compilable language that you may have access to use (if available) that does the same thing, compile it, run it, and see if the same behavior results.

    Is the executable name out of the ordinary? I know some malware tools do consider the filename as part of a threat assessment. Maybe try renaming the .exe file to something else and see what happens.

    I'm still leaning towards some sort of protective software/settings on your PC interfering. I'm completely convinced that NTFS permissions are not the issue. Unfortunately, I haven't had nearly as much experience with nuances of Windows 10 "protective helpfulness" as I have with older versions of Windows, so at this point I can't offer any additional suggestions.

  20. #20
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,191

    Re: Access Denied deleting file I own in VB.net

    If you have a version of Microsoft Office installed, it might not hurt to try creating a VBA macro that performs the same functions to see if it will work. If it doesn't, that might help with our diagnosis.

  21. #21

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    This is actually a darn good idea. I couldn't get it to copy to the same directory as FileCopy from VBA apparently doesn't like to copy to the same path as source and kept erroring out. So I created a subdirectory, copied the 01.jpg file there. I was then able to "kill" the original 01.jpg file and move via "Name" the copied file back to _Temp directory. I'm actually convinced the problem is with VS being corrupt somehow. So I've already 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, which currently fails as well. If that's successful I can then add the G partition and try the code on _Temp directory. If not I can restore image of the new clean install and then try to install VS 2022 and try it again. Hopefully I can get this done by Friday or Saturday and report back the results.
    Travel as much as you can, as often as you can and as far as you can

  22. #22
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,191

    Re: Access Denied deleting file I own in VB.net

    Another possibility is that the user account, not the VB/VS installation is corrupt. That can mess with file permissions also. Try creating another user account, make sure the new account has permissions for the Drive and Folder(s), and see what happens.

  23. #23

    Thread Starter
    New Member
    Join Date
    Nov 2022
    Posts
    13

    Re: Access Denied deleting file I own in VB.net

    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.
    Travel as much as you can, as often as you can and as far as you can

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width