Win32Exception:目录名无效模拟,UAC
本文关键字:模拟 UAC 无效 Win32Exception | 更新日期: 2023-09-27 18:03:22
我编写了一个程序,需要通过提升运行来调用WMI对象的两个方法。如果由管理员组中的用户以"提升模式"运行,则效果非常好。但现在的问题是,它需要由每个人运行,而不是每个人都在他们工作的机器上拥有管理权限。所以我决定让程序重新启动自己,假装一个来自"域管理员"组的用户。
我的代码如下:
ProcessStartInfo proc = new ProcessStartInfo();
proc.WorkingDirectory = Environment.CurrentDirectory;
proc.FileName = System.Windows.Forms.Application.ExecutablePath; ;
proc.UserName = "USERNAME";
SecureString secure = new SecureString();
foreach (var item in "PASSWORD")
{
secure.AppendChar(item);
}
proc.Password = secure;
proc.Domain = "DOMAIN";
proc.UseShellExecute = false;
proc.Verb = "runas";
Process.Start(proc);
启用UAC时有两个问题。首先,如果我没有显式地将应该被模拟的用户添加到程序的安装文件夹的ACL(用户已经在Administrators组中,该组对文件夹和其中包含的所有文件具有完全访问权限),它会抛出System.ComponentModel.Win32Exception: The directory name is invalid
。我仔细检查了目录路径,它实际上是有效的(并且当用户显式地在ACL中时工作)。
现在,如果用户是显式添加的,并且没有异常,则程序将以用户身份运行,但没有提升的权限,因此调用无法工作。
我做错了什么?谢谢你的帮助。
我知道已经有一些关于类似问题的问题,但是没有一个解决方案/答案解决了我的问题,或者它们不够详细,而且太长时间不活跃。
我一直有同样的问题,我无法同时提升和提供凭据。为了使用'runas'动词,你必须设置UseShellExecute=true,但为了提供凭据,你必须将其设置为false。我最终做的是测试管理员权限,如果不存在,则作为具有本地管理员权限的单独用户重新启动我的应用程序。然后我会运行这个过程。从"runas"动词开始,不需要提供凭据。此时,您的用户将收到一个UAC提示,选择yes后,该进程将以提升的方式运行。我是用VB写的,但想法是一样的,这是我的代码,IsUserAdminGroup是从:
使用的UAC self-elevation
Try
'check if user is administrator
Dim fInAdminGroup As Boolean = Me.IsUserInAdminGroup
If (Me.IsUserInAdminGroup = False) Then
'check if process is running under user you want to elevate
If Not (System.Environment.UserName = "") Then
'this process restarts the app as the desired admin user
Dim path As String = Environment.CurrentDirectory
Dim filename As String = Application.ExecutablePath
'create secure string password
Dim passwordPre As String = ""
Dim passwordChars As Char() = passwordPre.ToCharArray()
Dim password As New SecureString()
For Each c As Char In passwordChars
password.AppendChar(c)
Next
Dim procInfo As New System.Diagnostics.Process
procInfo.StartInfo.FileName = filename
procInfo.StartInfo.UserName = ""
procInfo.StartInfo.Domain = ""
procInfo.StartInfo.Password = password
procInfo.StartInfo.UseShellExecute = False
'load user profile was needed for a legacy application I used that failed without the user hive loaded
'procInfo.StartInfo.LoadUserProfile = True
procInfo.StartInfo.CreateNoWindow = True
procInfo.StartInfo.WorkingDirectory = path
procInfo.Start()
Application.Exit()
Else
MsgBox("Unable to open, call support.", MsgBoxStyle.Critical, "Run Failed")
Application.Exit()
End If
ElseIf (System.Environment.UserName = "") Then
Me.Visible = False
Me.Hide()
'this process starts the desired app elevated
Dim procInfo As New System.Diagnostics.Process
procInfo.StartInfo.FileName = "ptpublisher.exe"
procInfo.StartInfo.Verb = "runas"
procInfo.StartInfo.UseShellExecute = True
procInfo.StartInfo.CreateNoWindow = True
procInfo.StartInfo.WorkingDirectory = "C:'Program Files (x86)'Primera Technology'PTPublisher"
procInfo.StartInfo.Arguments = ""
procInfo.Start()
Application.Exit()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Run Failed")
Application.Exit()
End Try