.net - 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 Files和ProgramData文件夹。
这个更新过程的工作流程是这样的。
- MyApplication.exe在 Windows 启动时启动。
- 用户登录到MyApplication。
- 显示可用更新窗口,用户单击立即更新按钮。
- 以管理员身份提示
UAC
并启动updater.exe 。然后终止MyApplication.exe - updater.exe将下载 zip 文件并将它们解压缩到正确的文件夹中。
- 然后更新程序将再次启动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
解决方案
我仍然想了解发生这种情况的原因,但我找到了解决方案,
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
现在异常没有出现。
推荐阅读
- firebase - 无法在 firebase 云函数中创建自定义令牌,因为服务帐户没有必要的权限
- php - 在查询中指示数据库名称?
- python - Selenium 打开浏览器,但在 Chrome 和 Firefox 中都没有获取 URL
- python - 在 Iterator next 中优雅地捕获 python 异常
- c# - 使用 LINQ 获取两个日期之间的日期发生次数
- javascript - CSS 不影响使用 API Open Weather Map 的 Javascript 中的文本
- javascript - 如何将数据从一个浏览器发送到另一个浏览器
- python - 如何在 ScalarFormatter 之后更改对数图的 xticks 和 yticks?
- c# - 如何向单个动态生成的对象添加点击事件?
- python-3.x - 播放器在屏幕上正确时闪烁,python和pygame