我有一个Visual Studio Windows应用程序项目.我添加了代码来下载安装程序更新文件.完成下载后的安装程序需要管理员权限才能运行.我添加了一个清单文件.
当用户单击DownloadUpdate.exe时,UAC会提示用户输入管理员权限.所以我假设在DownloadUpdate.exe中创建和调用的所有进程都将以管理员身份运行.所以我使用以下代码调用我的下载文件:
Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = strFile;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
Run Code Online (Sandbox Code Playgroud) 从未以管理员身份运行的应用程序,我有以下代码:
ProcessStartInfo proc = new ProcessStartInfo();
proc.WindowStyle = ProcessWindowStyle.Normal;
proc.FileName = myExePath;
proc.CreateNoWindow = false;
proc.UseShellExecute = false;
proc.Verb = "runas";
Run Code Online (Sandbox Code Playgroud)
当我调用Process.Start(proc)时,我没有弹出请求以管理员身份运行的权限,并且exe不以管理员身份运行.
我尝试将app.manifest添加到myExePath中找到的可执行文件,并将requestedExecutionLevel更新为
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Run Code Online (Sandbox Code Playgroud)
使用更新的app.manifest,在Process.Start(proc)调用中,我得到一个异常,"请求的操作需要提升."
为什么.Verb操作没有设置管理员权限?
我正在测试Windows Server 2008 R2 Standard.
我有一个用C#编写的应用程序,需要能够在Windows中配置网络适配器.我有这个基本上通过WMI工作,但有一些我不喜欢的解决方案:有时设置似乎没有坚持,并且当没有插入网络电缆时,WMI返回错误方法,所以我不知道他们是否真的成功了.
我需要能够通过网络连接配置所有可用的设置 - 属性 - TCP/IP屏幕.
最好的方法是什么?
我想启动一个具有提升权限但具有隐藏窗口的子进程(实际上是相同的控制台应用程序).
我做下一个:
var info = new ProcessStartInfo(Assembly.GetEntryAssembly().Location)
{
UseShellExecute = true, // !
Verb = "runas",
};
var process = new Process
{
StartInfo = info
};
process.Start();
Run Code Online (Sandbox Code Playgroud)
这工作:
var identity = new WindowsPrincipal(WindowsIdentity.GetCurrent());
identity.IsInRole(WindowsBuiltInRole.Administrator); // returns true
Run Code Online (Sandbox Code Playgroud)
但是UseShellExecute = true创建了一个新窗口,我也无法重定向输出.
所以我下次做的时候:
var info = new ProcessStartInfo(Assembly.GetEntryAssembly().Location)
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false, // !
Verb = "runas"
};
var process = new Process
{
EnableRaisingEvents = true,
StartInfo = info
};
DataReceivedEventHandler actionWrite …Run Code Online (Sandbox Code Playgroud) 我正在尝试运行外部可执行文件,但显然它需要提升.代码是这样的,从使用ProcessBuilder的示例修改(因此具有一个参数的数组):
public static void main(String[] args) throws IOException {
File demo = new File("C:\\xyzwsdemo");
if(!demo.exists()) demo.mkdirs();
String[] command = {"C:\\fakepath\\bsdiff4.3-win32\\bspatch.exe"};
ProcessBuilder pb = new ProcessBuilder( command );
Process process = pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:\n", Arrays.toString(command));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
try {
int exitValue = process.waitFor();
System.out.println("\n\nExit Value is " + exitValue);
} catch (InterruptedException e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我有一个updater exe,旨在关闭主exe,用更新的exe替换它,然后启动更新的exe.当updater尝试启动更新的exe时,如果用户拒绝UAC权限对话框,则更新程序将挂起.这是因为Process.Start()函数永远不会返回.我的CPU周期表显示几乎没有使用btw.
我希望我的所有用户只对UAC说"是",但由于我在这里,我想至少用某种错误信息来处理这种情况.假设我的用户至少拥有Windows 7.这些exes本身就是32位Winforms应用程序.有针对性的.Net Framework是4.0.使用Visual Studio 2010 Ultimate.
有关如何检测用户何时拒绝UAC对话框的任何想法?
我猜我所能做的就是让Process.Start()一个单独的线程运行一段时间后会超时.有关更多代码:
private void RestartProcess()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Users\Me\Documents\Visual Studio 2010\Projects\updated.exe";
MessageBox.Show("Attempting to start process");
Process newProc = Process.Start(startInfo);
MessageBox.Show("If this shows, the user has clicked YES in the UAC.");
}
Run Code Online (Sandbox Code Playgroud)
解:
Process.Start()除非使用Try {} Catch {}块来捕获错误,否则使用Win32Exception静默退出.
我想允许用户从我的非管理员程序中以管理员身份运行命令行实用程序,并让我的程序获取输出.该实用程序是第三方,但随我的程序一起分发.
我可以重定向程序的输出,我可以作为管理员运行程序,但我不能同时做两个.
我目前唯一可以工作的是使用cmd.exe将输出重定向到文件,例如:
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Reflection;
string appDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string utilityPath = Path.Combine(appDirectory, "tools", "utility.exe");
string tempFile = Path.GetTempFileName();
Process p = new Process();
// hide the command window
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "cmd.exe";
// run the tool, redirect the output to the temp file and then close.
p.StartInfo.Arguments = " /C \"\"" + utilityPath + "\" > \"" + tempFile + "\"\"";
p.StartInfo.Verb = "runas"; // run as …Run Code Online (Sandbox Code Playgroud) 我试图通过C#应用程序执行具有管理员权限的程序,该应用程序仅使用用户权限进行调用.
码
ProcessStartInfo psi;
try
{
psi = new ProcessStartInfo(@"WINZIP32.EXE");
psi.UseShellExecute = false;
SecureString pw = new SecureString();
pw.AppendChar('p');
pw.AppendChar('a');
pw.AppendChar('s');
pw.AppendChar('s');
pw.AppendChar('w');
pw.AppendChar('o');
pw.AppendChar('r');
pw.AppendChar('d');
psi.Password = pw;
psi.UserName = "administrator";
Process.Start(psi);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
它确实启动winzip,但仅限用户权限.有什么我做错了,或者甚至有可能开始一个更高权利的过程?
谢谢!
编辑:这是问题背后的原因,也许它有助于理解我真正需要的东西.
我以winzip为例,大致了解了我的代码是不正确的.实际问题是,我们公司使用2个版本的程序.但在启动任何版本之前,您需要使用regsvr32(具有管理员权限)导入dll文件.现在我想编写一个程序,让用户选择版本,导入dll并启动正确的应用程序.
我需要杀死Windows资源管理器的进程(explorer.exe)
让我说我使用本机NT方法TerminateProcess
它的工作原理,但问题是探险家再次启动,无论如何,可能是Windows正在这样做.当我用Windows任务管理器杀死explorer.exe时,它不会回来,它会被杀死.
我想做任务管理员通过我的申请做的事情.
编辑:
感谢@sblom我解决了它,在注册表中快速调整诀窍.虽然它是一个聪明的黑客,显然有任务的人有一个更干净的方式,这就是说,我决定现在用@ sblom的方式.
有人知道如何在运行时获得管理员权限吗?请不要建议这样做:
<requestedPrivileges>
<requestedExecutionLevel
level="requireAdministrator"
uiAccess="false"/>
</requestedPrivileges>
Run Code Online (Sandbox Code Playgroud)
希望有人知道如何做到这一点。
我的意思是,例如,考虑这种情况:我有一个在没有管理员权限的情况下运行的应用程序,通常不会在您的计算机上进行任何更改。但是,如果您单击某个按钮,该应用程序将请求管理员权限,然后执行一些必要的操作。