I need my app to require admin rights before running a process asynchronously. It used to work well with the following configuration in the app.manifest:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
However, since now processes were added that should not require admin rights to be run, this elegant solution no longer cuts it. I expected this to do the trick:
process.StartInfo.UseShellExecute = true;
process.StartInfo.Verb = "runas";
This is what I have, but there must be an error somewhere, since this code runs the process as expected, but doesn't actually require admin rights to do so:
public async Task ExecuteElevatedProcessAsync(string executablePathArg)
{
using (var process = new Process())
{
process.StartInfo.FileName = executablePathArg;
process.StartInfo.UseShellExecute = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Verb = "runas";
await RunAsync(process);
};
}
private Task RunAsync(Process processArg)
{
var taskCompletionSrc = new TaskCompletionSource<object>();
processArg.EnableRaisingEvents = true;
processArg.Exited += (s, e) => taskCompletionSrc.TrySetResult(null);
if (!processArg.Start())
{
taskCompletionSrc.SetException(new Exception("Some descriptive error-message."));
}
return taskCompletionSrc.Task;
}
Do you know how to fix this?
By running it on another computer I’ve learned that the above code does indeed work as intended: Before executing the asynchronous process, it triggers the Windows UAC-popup that asks the user to give the app permission to make changes to the device.
The popup was never triggered on my Windows machine because the user account control was disabled entirely, which isn’t a smart choice to begin with...
Related
I figure out how to launch a process. But my problem now is the console window (in this case 7z) pops up frontmost blocking my vision and removing my focus interrupting my sentence or w/e i am doing every few seconds. Its extremely annoying, how do i prevent that from happening. I thought CreateNoWindow solves that but it didnt.
NOTE: sometimes the console needs user input (replace file or not). So hiding it completely may be a problems a well.
This is my current code.
void doSomething(...)
{
myProcess.StartInfo.FileName = ...;
myProcess.StartInfo.Arguments = ...;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
myProcess.WaitForExit();
}
If I recall correctly, this worked for me
Process process = new Process();
// Stop the process from opening a new window
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
// Setup executable and parameters
process.StartInfo.FileName = #"c:\test.exe"
process.StartInfo.Arguments = "--test";
// Go
process.Start();
I've been using this from within a C# console application to launch another process, and it stops the application from launching it in a separate window, instead keeping everything in the same window.
#galets
In your suggestion, the window is still created, only it begins minimized. This would work better for actually doing what acidzombie24 wanted:
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Try this:
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
I'll have to double check, but I believe you also need to set UseShellExecute = false. This also lets you capture the standard output/error streams.
In my C# WPF Application in statup process I'm checking whether the installation of new version is required. If so, I want to break current process and start installer. The installer is developed using NSIS package.
The problem is that sometimes only User Account Control dialog from NSIS installer appears and install process breaks.
How to ensure that the installation process is executed every time?
Here is my code on Application Startup.
protected override void OnStartup(StartupEventArgs e)
{
try
{
//Disable shutdown when the dialog closes
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
if ( IfUpdateRequired())
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(sessionCtx.AutoUpdateVersionInfo.SetupPath);
//This should not block current program
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
System.Diagnostics.Process.Start(startInfo);
this.Shutdown();
}
else
{
base.OnStartup(e);
}
}
catch (Exception ex)
{
}
}
My only guess is that the child process has not fully started before your process quits. ShellExecute is allowed to perform its operation asynchronously.
If this is the cause then you should be able to work around it by sleeping a bit before calling this.Shutdown(). Wait 10 seconds or so perhaps? Or call WaitForInputIdle(9999) on the process. Or maybe you could check the Responding process property?
I am writing a C# winform application, that controls a service. I tried to control this service with System.Diagnostics.Process and System.ServiceProcess.ServiceController.
Because of the required AdminRights to make changes at a service and a few post i read about make this happen with the "runas"-verb, I decided to use System.Diagnostics.Process.
System.Diagnostics.ProcessStartInfo startInfo =
new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C net start " + SERVICE_NAME;
startInfo.RedirectStandardError = true; //// <<<<<<<<<<<<<<<<<
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
Process process = new Process();
process.StartInfo = startInfo;
process.ErrorDataReceived += cmd_DataReceived;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
But it seems that the runas-Verb and the redirection of the StandardErrorOutput doesn't work. It works if I comment out the line, but I need this to decide if the command was executed successfully or not.
Is there a way how to start/ stop a service (executed with temporary admin rights) and to get a result of success?
Your best option is to start the service yourself, inside your own application.
The problem of course is that your application normally isn't run as an administrator (nor do you want it to be). That means you can't start or stop services. That is why you should temporarily elevate, perform the task needed, then exit.
In other words, launch a second copy of your app, passing a command line option instructing yourself to start the service. The second copy does only that, then exits.
The Code
First we have a button the user can click to start the service. We first check if the user already is an administrator, in which case we don't have to do anything special:
private void button1_Click(object sender, EventArgs e)
{
//If we're an administrator, then do it
if (IsUserAnAdmin())
{
StartService("bthserv"); //"Bluetooth Support Service" for my sample test code
return;
}
//We're not running as an administrator.
//Relaunch ourselves as admin, telling us that we want to start the service
ExecuteAsAdmin(Application.ExecutablePath, "/serviceStart");
}
//helper function that tells us if we're already running with administrative rights
private Boolean IsUserAnAdmin()
{
//A user can be a member of the Administrator group, but not an administrator.
//Conversely, the user can be an administrator and not a member of the administrators group.
//Check if the current user has administrative privelages
var identity = WindowsIdentity.GetCurrent();
return (null != identity && new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator));
}
private void ExecuteAsAdmin(string Filename, string Arguments)
{
//Launch process elevated
ProcessStartInfo startInfo = new ProcessStartInfo(Filename, Arguments);
startInfo.Verb = "runas"; //the runas verb is the secret to making UAC prompt come up
System.Diagnostics.Process.Start(startInfo);
}
Then, during startup, we just need to check if we're being called with the command line option. If so, start the service:
public Form1()
{
InitializeComponent();
//Ideally this would be in program.cs, before the call to Application.Run()
//But that would require me to refactor code out of the Form file, which is overkill for a demo
if (FindCmdLineSwitch("serviceStart", true))
{
StartService("bthserv"); //"Bluetooth Support Service"
Environment.Exit(0);
}
}
private bool FindCmdLineSwitch(string Switch, bool IgnoreCase)
{
foreach (String s in System.Environment.GetCommandLineArgs())
{
if (String.Compare(s, "/" + Switch, IgnoreCase) == 0)
return true;
if (String.Compare(s, "-" + Switch, IgnoreCase) == 0)
return true;
}
return false;
}
And finally to start the service:
private void StartService(String ServiceName)
{
TimeSpan timeout = TimeSpan.FromMilliseconds(30000); //30 seconds
using (ServiceController service = new ServiceController(ServiceName))
{
try
{
service.Start();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error starting service");
return;
}
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
Extra fancy
You can add arbitrary amounts of fancy code. Detect if the user is not an admin, and if not then use the proper Windows API (BCM_SETSHIELD message) to add a UAC shield to your button:
That way users know to expect UAC to appear; because you followed the Microsoft UI guidelines:
Guidelines
UAC shield icon
Display controls with the UAC shield to indicate that the task requires immediate elevation when UAC is fully enabled, even if UAC isn't currently fully enabled. If all paths of a wizard and page flow require elevation, display the UAC shield at the task's entry point. Proper use of the UAC shield helps users predict when elevation is required.
Bonus Reading
BCM_SETSHIELD message
Windows Design Guidelines - User Account Control
Designing UAC Applications for Windows Vista (Especially Step 4: Redesign Your UI for UAC Compatibility)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa511445.aspx
Note: Any code is released into the public domain. No attribution required.
You could use MT.exe which is a utility provided by the framework ( In the SDK sub-directory ) to generate a manifest that forces your application to run in administrator mode. In which case you shouldn't have problem dealing with your protected service. The trade off of course is that ... you're running in admin mode.
mt.exe -manifest "your-path\your-app.exe.manifest" -updateresource:"$(TargetDir)$(TargetName).exe;#1
Hopefully this helps.
I already know how to catch standard output of a console window, BUT my problem is the case when I get the process with GetProcesses/orByName and do not Start() it myself. Here is the code:
public ProcessCaller(ISynchronizeInvoke isi, Process MárFutóAlkalmazás)
: this(isi)
{
//alapbeállítások
FileName = MárFutóAlkalmazás.StartInfo.FileName;
Arguments = MárFutóAlkalmazás.StartInfo.Arguments;
WorkingDirectory = MárFutóAlkalmazás.StartInfo.WorkingDirectory;
//egyedi beállítások
process = MárFutóAlkalmazás;
process.EnableRaisingEvents = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
new MethodInvoker(ReadStdOut).BeginInvoke(null, null);
new MethodInvoker(ReadStdErr).BeginInvoke(null, null);
//események
StdErrReceived += new DataReceivedHandler(Loggolás);
StdOutReceived += new DataReceivedHandler(Loggolás);
//kilépés jelzése
process.Exited += new EventHandler(OnKilépés);
}
So this method gets and already running application as MárFutóAlkalmazás parameter. Sets some internal properties, then hooks to Output. However when it comes to
StdOutReceived += new DataReceivedHandler(Loggolás);
and the program runs the Loggolás method to take the console data, it says that the StandardOut is not set, or the process is not started.
Well:
StandardOut is set
Process is running, since I get it by GetProcesses
In this routine I do NOT use process.Start() - since it is started already
Looking for help. Thank yas:
Péter
Ok, so after asking around and checking on net, I learned that you can not hook on an output not started by you. So if your executor application crashes, you will have to restart console application to be able to capture output. You need the .Start().
Actually I see only one salvation for this problem: starting with the ">filename.txt" or such output redirecting parameter. This will stuff everything into a file, so even if executor application crashes you can "reconnect" if you "read only". Did not tested this yet, but I see no other way.
I am attempting to wrap a 3rd party command line application within a web service.
If I run the following code from within a console application:
Process process= new System.Diagnostics.Process();
process.StartInfo.FileName = "some_executable.exe";
// Do not spawn a window for this process
process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;
// Redirect input, output, and error streams
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.EnableRaisingEvents = true;
process.ErrorDataReceived += (sendingProcess, eventArgs) => {
// Make note of the error message
if (!String.IsNullOrEmpty(eventArgs.Data))
if (this.WarningMessageEvent != null)
this.WarningMessageEvent(this, new MessageEventArgs(eventArgs.Data));
};
process.OutputDataReceived += (sendingProcess, eventArgs) => {
// Make note of the message
if (!String.IsNullOrEmpty(eventArgs.Data))
if (this.DebugMessageEvent != null)
this.DebugMessageEvent(this, new MessageEventArgs(eventArgs.Data));
};
process.Exited += (object sender, EventArgs e) => {
// Make note of the exit event
if (this.DebugMessageEvent != null)
this.DebugMessageEvent(this, new MessageEventArgs("The command exited"));
};
process.Start();
process.StandardInput.Close();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
int exitCode = process.ExitCode;
process.Close();
process.Dispose();
if (this.DebugMessageEvent != null)
this.DebugMessageEvent(this, new MessageEventArgs("The command exited with code: " + exitCode));
All events, including the "process.Exited" event fires as expected. However, when this code is invoked from within a web service method, all events EXCEPT the "process.Exited" event fire.
The execution appears to hang at the line:
process.WaitForExit();
Would anyone be able to shed some light as to what I might be missing?
As it turns out the problem was caused by the executable that I was trying to invoke.
Unfortunately, this 3rd party executable was a port of a UNIX command that was being run through a kind of emulator. The executable was designed to output messages to the output and error streams as one would expect. However, the toolkit our vendor used to port the binaries over to Windows does not use the standard output streams.
When I stepped through the web service and manually invoked the process from the command line, I saw the emulator display an error dialog box. From the C# point of view, the process does not complete unless the [OK] button is clicked on the dialog box hence the "Exited" event never fires.
After speaking with our vendor for the executable I learned it is not fully supported in 64-bit Windows. I installed the web service on a 32-bit environment and everything was fine.
What's the process you are running in there? Since you mentioned its a console application, perhaps, it was waiting for more input? Since running this as a webservice, is the executable running under the same permissions as the ASP webservice? It could be that the web-service is not releasing the executable as that is loaded in process, or that the web-service is designated to run forever until IIS gets restarted and then the Process's Exit event may get fired.
It has come to my attention also, that there is no using clause around the Process's instantiated object i.e.
using (Process proc = new Process())
{
}
Edit: Also, please see here a link to a similar concept. The only thing after comparing the results is that the property WindowStyle is set...
ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;