I am trying to delete a mdb file from “Program Files (x86)” folder and it denies the access permission. When my application installs in a machine, the mdb file is placed in the “Program Files (86)” folder and while running the application, it tries to delete it from that location. But it shows access permission denied error message. I am using VB.Net windows application. Visual Studio 2022 and OS is Win 10 64 bit.
I tried the below ways.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim path1 As String = "C:Program Files (x86)MyCOMPANIESMyDB.mdb"
If File.Exists(path1) Then
My.Computer.FileSystem.DeleteFile(path1, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.DoNothing)
End If
End Sub
The above code doesnt throw any error but asks for entering admin credentials. This is NOT acceptable in my case.
And I tried as shows below :
Dim path1 As String = "C:Program Files (x86)MyCOMPANIESMyDB.mdb"
File.SetAttributes(path1, FileAttributes.Normal)
File.Delete(path1)
It threw below shown error:
System.UnauthorizedAccessException: 'Access to the path 'C:Program Files (x86)MyCOMPANIESMyDB.mdb' is denied.'
So, I thought about a new way that i tried to move the file from program files location to some other location and delete the file from there. So I tried below code.
Dim path1 As String = "C:Program Files (x86)MyCOMPANIESMyDB.mdb"
Dim filename As String = Path.GetFileName(path1)
Dim destinationPath As String = Path.Combine("C:MyFolderMyDB.mdb", filename)
If File.Exists(destinationPath) Then
File.Delete(destinationPath)
End If
File.Move(path1, destinationPath)
It also threw error as shown below:
System.UnauthorizedAccessException: 'Access to the path is denied.'
How to delete that file from Program Files (86) location using VB.Net code ??
Please help.