Run new process as user interacting with UI - c#

I have a GUI process that is running as nt authority\system. I'd like to launch another process (preferably via Process class) as the user that is interacting with the GUI process. If I just call Process.Start the new process will also run as nt authority\system but I want it to be domain\user.
Edit: for clarification, I don't have the current user's username or password. I just want to run the process as though the user was starting it themselves without having to ask for username/password.

Use StartInfo property with a valid credentials.
Process proc = new Process();
proc.StartInfo.Domain = "YourDomain";
proc.StartInfo.UserName = "Username";
proc.StartInfo.Password = "YourPassword";

You can do this:
Process proc = new System.Diagnostics.Process();
System.Security.SecureString ssPwd = new System.Security.SecureString();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "C:/YourPath/YourProcess.exe";
proc.StartInfo.Arguments = "Args"; //Arguments if any, otherwise delete this line
proc.StartInfo.Domain = "domainname";
proc.StartInfo.UserName = "username";
proc.StartInfo.Password = "password";
proc.Start();

var psi = new ProcessStartInfo();
psi.Verb = "runas";
psi.FileName = "notepad.exe";
Process.Start(psi);

'run as' argument while invoking a process with Process object, will ask for password of the user.
It is not possible to run a process as User account from a Local System Account.
You can do a workaround to solve your problem
Start a Main Process 'A' as the logged on user.
Now, start the required Local System Account 'B' process from the Main Process(A).
Now 'A' will monitor for a trigger from process B, if the main code (the code that must be run in user account) needs to be run. If the trigger hits, the required code can be run.
Monitoring can be done by monitoring(reading) a text file for every certain duration, and the trigger can be sent from B(system account process) which is writing to the text file. Any other better monitoring approach can be taken.

Related

How to differentiate multiple processes with the same name and to kill all those processes which are running with my USERNAME in C#?

I have multiple process named COM SURROGATE (dllhost.exe) are running in Task Manager at my System. In which I need to kill all those processes which are running with my USERNAME(One is running with SYSTEM/"" so no need to kill that).
I need to do like below but only for the dllhost process running with myusername :
Process[] runningProcess = Process.GetProcessesByName("dllhost");
if(runningProcess.Length > 0 )
{
foreach (var surrogateProcess in runningProcess)
{
surrogateProcess.Kill();
}
}
I found the solution for this. Below are the key points :
We can't close process running by SYSTEM/""/otheruser etc without admin access so process.kill() used to throw Access Denied error.
By using below we are trying to terminate all the process with the name of dllhost.exe (we can write any of the process name) and used Style.Hidden so user will not see the cmd prompt and not even the message.
Message could be like :
ERROR: The process "dllhost.exe" with PID 6332 could not be terminated.
Reason: Access is denied. [This is running with either system or other user]
SUCCESS: The process "dllhost.exe" with PID 15320 has been terminated. [This was running with my username which can be closed without any issue]
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C taskkill /IM dllhost.exe /F";
process.StartInfo = startInfo;
process.Start();
The code will close all the processes running with my username only. Cheers.

Process.start on network drive, ask for authentication

Considering this code:
Process process = new Process();
process.StartInfo.FileName = "explorer";
process.StartInfo.Arguments = "\\some_network_host\path";
process.Start();
I would like to connect to a shared resource and open the path in Explorer.exe, however, the user might not be authenticated yet. If the user is not authenticated, I would like to open a Windows authentication popup just like the one I see when I run \\some_network_host\path, however, my actual code just opens "My Document" instead (if the user is not already authenticated). If the user is already authenticated, it opens the explorer.exe window showing the shared resource.
Thank you.
This code works fine for me
Process process = new Process();
process.StartInfo.FileName = #"\\existing_network_host\path";
process.StartInfo.UseShellExecute = true;
process.StartInfo.ErrorDialog = true;
process.Start();
The keey difference is true value for StartupInfo.ErrorDialog

Running cmd commands with Administrator rights

How can I run the command **cd..** behind the scenes of a Windows Form? (i.e. the user cannot see it)
Thanks.
See System.Diagnostics.Process http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
There is also this SO answer to this same exact question: https://stackoverflow.com/a/1469790/25882
Example:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
You may initialize a new System.Diagnostics.ProcessStartInfo which has the information required for your process to start in addition to WindowStyle that indicates the window state to use when the process is started which can be Hidden, Maximized, Minimized or Normal. In your case, we will set this as Hidden so that the process which will be started won't be able to receive either input nor show the output from/to the user.
Example
System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + #"\System32\cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%\System32\cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo
Screenshot
The following screenshot represents the Task Manager showing one process which was started by our application. However, its Window is not visible.
Notice: The process started will not terminate even if you close your application.
Additionally, to run a Process as an Administrator you may set the Verb property of the process start info to runas
Example
System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + #"\System32\cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%\System32\cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
myProcessInfo.Verb = "runas"; //The process should start with elevated permissions
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo
Notice: If you have the User Account Control enabled, you may be asked to allow the process to start with elevated permissions first if the application that tried to call this process was not running with elevated permissions.
If you would like to skip the prompt, I think that you should allow your main application to start with elevated permissions. To do this, you'll need to open your application's manifest and make sure that the following line is added
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
This will simply tell your application to start only with elevated permissions. So, when you call the process as an Administrator, there'll be no prompt as the process caller is being executed under an Administrator.
Thanks,
I hope you find this helpful :)

Process.Start creates a pop window

I've an exe which runs a process to open the DeviceManager. But unfortunately, it asks for a confirmation to provide 'yes' or 'No' which waits for user input for long time and does not continue with execution.
How to get rid of this? I do not want to provide a confirmation again as I do not want to pause the EXE run with this.
StartInfo.CreateWindow = false would not hold for this as it just for starting in another cmd window.
Code below:
Process p = new Process();
p.StartInfo.FileName = "devcon.exe";
p.StartInfo.CreateNoWindow = false;
p.Start();
The messagebox you are seeing is UAC (User Account Control) which was implemented since Vista.
To bypass the box you might be able to try providing the credentials programmatically before launching the process, I can't test but something like this:
var processInfo = new ProcessStartInfo
{
FileName = "devcon.exe",
UserName = "Administrator",
Domain = "yourdomain or leave blank",
Password = adminpassword,
UseShellExecute = false,
};
Process.Start(processInfo);
Otherwise the user will have to have admin rights, or the password!
The other option would be to disable UAC. However that wouldn't allow the user to do anything they couldn't do normally, it will probably tell you that you can't make any changes without the process running as admin.
The parent process should be run by administrator account.
Also show all code please.

Mimic Windows' 'Run' window in .NET

I would like to mimic the Run command in Windows in my program. In other words, I would like to give the user the ability to "run" an arbitrary piece of text exactly as would happen if they typed it into the run box.
While System.Diagnostics.Process.Start() gets me close, I can't seem to get certain things like environment variables such as %AppData% working. I just keep getting the message "Windows cannot find '%AppData%'..."
You can use the Environment.ExpandEnvironmentVariables method to turn %AppData% into whatever it actually corresponds to.
Depending on what you're trying to do, you could also call CMD.EXE, which will expand your environment variables automatically. The example below will do a DIR of your %appdata% folder, and redirect the stdOut to the debug:
StreamReader stdOut;
Process proc1 = new Process();
ProcessStartInfo psi = new ProcessStartInfo("CMD.EXE", "/C dir %appdata%");
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
proc1.StartInfo = psi;
proc1.Start();
stdOut = proc1.StandardOutput;
System.Diagnostics.Debug.Write(stdOut.ReadToEnd());

Categories