首页 > 解决方案 > ProgramData 文件夹在另一个进程 (updater.exe) 访问后无法访问

问题描述

我在C:\ProgramFiles\MyAPP\MyApplication.exe中运行了一个 exe ,这个应用程序在启动时运行 (HKLM: SOFTWARE\Microsoft\Windows\CurrentVersion\Run)SQLite 数据库和其他配置文件位于C:\ProgramData\MyAppSettings文件夹

我正在使用安装应用程序,Inno Setup并且已使用此代码段将ProgramData\MyAppSettings文件夹权限设置为。users-full

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

我也有一个updater.exe,它下载最新的更新(作为 2 个 zip 文件)并将它们解压缩到Program FilesProgramData文件夹。

这个更新过程的工作流程是这样的。

  1. MyApplication.exe在 Windows 启动时启动。
  2. 用户登录到MyApplication
  3. 显示可用更新窗口,用户单击立即更新按钮。
  4. 以管理员身份提示UAC并启动updater.exe 。然后终止MyApplication.exe
  5. updater.exe将下载 zip 文件并将它们解压缩到正确的文件夹中。
  6. 然后更新程序将再次启动MyApplication.exe 并自行终止。

除了一件事,所有这些事情都很好。

updater.exe解压后, MyApplication.exeProgramData上的zip 文件无法访问其中的文件

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

所以我尝试过这样的事情。在updater.exe终止之前,它授予访问权限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

不幸的是,结果是一样的。MyApplication.exe仍然无法访问ProgramData. 问题可能出在哪里?

文件夹和数据库文件权限

在此处输入图像描述 在此处输入图像描述

updater.exeProgramData在文件夹中做什么

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

标签: .netvb.net

解决方案


我仍然想了解发生这种情况的原因,但我找到了解决方案,

updater.exe完成更新过程后,我需要将ProgramData\MyAppSettings中的所有文件和文件夹设置为MyApplication.exe首次启动NORMAL时的属性(更新后首次启动)

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

然后我可以把它们藏起来(如果我想要的话)

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

现在异常没有出现。


推荐阅读