我编写了一个程序,需要提升运行权限才能调用 WMI 对象的两个方法。如果由管理员组中的用户在“提升模式”下运行,则可以像 super 按钮一样工作。但现在的问题是它需要由每个人运行,而不是每个人都在他们工作的机器上拥有管理权限。因此,我决定让程序自行重新启动,以模拟“域管理员”组中的用户。
我的代码如下:
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 的用户(用户已位于管理员组中,该组对文件夹及其中包含的所有文件具有完全访问权限),则会抛出 System .ComponentModel.Win32Exception:目录名称无效
。
我仔细检查了目录路径,它实际上是有效的(并且当用户明确位于 ACL 中时有效)。
现在,如果明确添加用户并且没有异常(exception),程序将以用户身份运行,但不具有提升的权限,因此调用不起作用。
我做错了什么?感谢您的帮助。
我知道已经有一些关于类似问题的问题,但没有一个解决方案/答案能解决我的问题,或者它们不够详细且长时间不活动。
最佳答案
我也遇到了同样的问题,我无法同时提升并提供凭据。为了使用“runas”动词,您必须设置 UseShellExecute=true,但为了提供凭据,您必须将其设置为 false。我最终做的是测试管理员权限,如果它们不存在,则以具有本地管理员权限的单独用户身份重新启动我的应用程序。然后我将使用“runas”动词运行 process.start,而无需提供凭据。此时,您的用户将收到 UAC 提示,选择"is"后,进程将提升运行权限。我用 VB 编写了我的代码,但想法是相同的,这是我的代码,IsUserAdminGroup 的使用来自:
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
关于c# - "Win32Exception: The directory name is invalid"模拟和UAC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356267/