How to start a c# process as another user? - c#

I want to start a c# process as another user.
I know the verb "runas" but that only accepts admin user creds.
//This code actually meant to restart the appplication with specific args
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Arguments = args;
startInfo.Verb = "runas"; //this line is the question
Process p = Process.Start(startInfo);
I want to achieve the same thing but also with non-admin users.

Related

c# run .bat file application as administrator do not start

I have to run a .bat file from c#...
I use this method.
file = "C:\\Diego\\PublishCore\\Startup_service.bat";
ProcessStartInfo psi = new ProcessStartInfo();
psi.CreateNoWindow = true;
psi.FileName = file;
psi.UseShellExecute = true;
psi.Verb = "runas";
Process.Start(psi);
.BAT is executed... but the action I ask to perfom it does not execute...
If my .bat says MKDir MyDir... Its creates a Directory called MyDIr with no problems.
But when my bat says dotnet myApp.dll, a cmd Windows opens and closes, but it does not start myApp aplication....
If a doublé-click my .bat is runs fine.
What I am missing? Why the aplication does not start?
I solved it...
The problem was that, as my bat run the instruction dotnet myApp.dll.
I set the path file where the file was, but it was executed in the location where the my Solution is, instead of running in the same directory where I have .bat file.
I have to set WorkingDirectory and Arguments
C:\\Diego\\PublishCore\\Startup_InomCore.bat
ProcessStartInfo psi = new ProcessStartInfo();
psi.WorkingDirectory = "C:\\Diego\\PublishCore";
// psi.CreateNoWindow = true;
psi.FileName = #"cmd.exe";
psi.Arguments = "/c start /wait " + "C:\\Diego\\PublishCore\\Startup_InomCore.bat";
// psi.UseShellExecute = true;
psi.Verb = "runas";
var process = Process.Start(psi);

How do I correctly start a process with administrator rights?

So I've looked at alot of different SO posts, I've been on codeproject and dreamincode aswell but I cant for the life of me find out how to CORRECTLY start a process on Windows 8.1 with admin rights.
This is my code.
I'm trying to create a silent install of FireFox but everytime I give it the filepath it still promts me with the UAC, and I thought that running as admin would work. What am I doing wrong here?
Console.WriteLine("Please enter the path to the application: ");
string path = Console.ReadLine();
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = true;
psi.Verb = "runas";
psi.Arguments = "/s /v /qn /min";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = path;
Process.Start(psi);

Run Command as another user C#

I have an app running as Network Service (I can't change this) and need to run a command (execute as bat script) as a known local user. I seem to get no response and the script doesn't execute. The odd thing is that if i run the code as the local user its works without an 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 = #"C:\Windows\System32\cmd.exe";
startInfo.Arguments = "/C " + #"c:\example\script.bat";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
var sspw = new SecureString();
foreach (var c in "MyPassword")
{
sspw.AppendChar(c);
}
startInfo.Domain = Environment.MachineName;
startInfo.UserName = "MyUser";
startInfo.Password = sspw;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
I believe you need to set the working directory when using a Username/Password
according to MSDN...
The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\system32.
Not sure whether this solution will help your need but if set the WorkingDirectory to current directory (".") then it should work. Basically the user who is running the process should have permission on the folder that you mention.

command prompt c# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Redirect Standard Output Efficiently in .NET
Capturing console output from a .NET application (C#)
I know how to execute something like this:
SomeEXE inputfile.txt
in the command prompt via C#.
The problem I am having is that SomeEXE opens another command prompt where it writes the outputs given inputfile.txt.
Is it generally possible to obtain these outputs? Thanks.
Here is my current code:
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 SomeEXE inputfile.txt";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
process.StartInfo = startInfo;
process.Start();
// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;
process.WaitForExit();
String line = outputReader.ReadToEnd();
ProcessStartInfo processStartInfo = new processStartInfo("SomeEXE", "inputfile.txt");
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
// Here is where you grab the output:
processStartInfo.RedirectStandardOutput = true;
Process process = new Process {
StartInfo = processStartInfo
};
process.Start();
// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;
process.WaitForExit();
Now you can read the outputStream as necessary.
I am guessing this is what you mean. Also, here are the docs on RedirectStandardOutput
Also, if you know the path to the file that was generated (assuming the SomeEXE wrote to another file) you can use File.Open to access its contents after SomeEXE has executed (remember to wait until after otherwise SomeEXE may still have a handle on the file making it difficult to read it).

How to pass command line argument in .net windows application

i want to pass a argument in c#.net to a console application i tried ProcessStartInfo
but that can be used for immediate run of an application ... but i want to set the arguments for the application which will run at scheduled time
Use the arguments propery to pass command line arguments
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments.aspx
Example:
var info = new System.Diagnostics.ProcessStartInfo();
info.FileName = "cmd.exe";
info.Arguments = "/C";
info.UseShellExecute = true;
var process = new System.Diagnostics.Process();
process.StartInfo = info;
process.Start();
process.WaitForExit();

Categories