Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgramData folder cannot access after another process (updater.exe) access it

Tags:

.net

vb.net

I have an exe runs in C:\ProgramFiles\MyAPP\MyApplication.exe and this app runs at startup (HKLM: SOFTWARE\Microsoft\Windows\CurrentVersion\Run) The SQLite database and other configuration files located at C:\ProgramData\MyAppSettings Folder

I’m installing the application using Inno Setup and I have set ProgramData\MyAppSettings folder permission to users-full by using this code snippet.

[Dirs]
Name: "{commonappdata}\MyAppSettings"; Permissions: users-modify users-full

I have a updater.exe too which downloads the latest updates (as 2 zip files) and extracts them to Program Files and ProgramData folders.

The workflow of this update process is like this.

  1. MyApplication.exe starts at windows start.
  2. User Login into the MyApplication.
  3. Show Updates Available window and the User clicks on the update now button.
  4. Prompt UAC and Launch updater.exe as Administrator. Then terminate the MyApplication.exe
  5. The updater.exe will download zip files and extract them to correct folders.
  6. Then the updater will start MyApplication.exe again and terminate themself.

All those things work fine except one thing.

After updater.exe extract, the zip file on ProgramData the MyApplication.exe cannot access files on it

System.UnauthorizedAccessException: 'Access to the path 'C:\ProgramData\MyAppSettings\Database.db' is denied.

So I have tried something like this. Before the updater.exe terminate, it grant access permission to Everyone Full Control

Private Sub GrantAccessPermission(ByVal fullPath As String)
        Dim dInfo As DirectoryInfo = New DirectoryInfo(fullPath)
        Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()
        dSecurity.AddAccessRule(New FileSystemAccessRule(New SecurityIdentifier(WellKnownSidType.WorldSid, Nothing), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit Or InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow))
        dInfo.SetAccessControl(dSecurity)
    End Sub

Unfortunately, the result is the same. MyApplication.exe still cannot access files in the ProgramData. Where could the problem be?

Folder and database file permission

enter image description here enter image description here

what updater.exe doing in the ProgramData folder

Using Zip As New Ionic.Zip.ZipFile(BaseCompSource)
   Dim Entry As ZipEntry
   For Each Entry In Zip
      Try

         Entry.ExtractWithPassword(BaseCompDestination, ExtractExistingFileAction.OverwriteSilently, Password)
         setTaskText(Entry.FileName)

      Catch ex As Exception
         Debug.WriteLine(ex.Message)
      End Try

   Next
End Using
like image 801
dins88 Avatar asked Dec 14 '25 21:12

dins88


1 Answers

I still tying to understand the reason why this happen, but I've found a solution,

After updater.exe done update process, I need to set all files and folders in ProgramData\MyAppSettings to NORMAL attribute at the MyApplication.exe first launch (first launch after update)

File.SetAttributes(sFile.FullName, FileAttributes.Normal)
MyDirectory.Attributes = FileAttributes.Directory

then I can hide them back (if only I want)

File.SetAttributes(sFile.FullName, File.GetAttributes(sFile.FullName) Or FileAttributes.Hidden Or FileAttributes.System)
MyDirectory.Attributes = FileAttributes.Directory Or FileAttributes.Hidden Or FileAttributes.System

Now the exception doesn't shows up.

like image 161
dins88 Avatar answered Dec 16 '25 14:12

dins88



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!