How to pass hidden input to another program in C #
I am having problems programmatically putting programs into a program net user
using C #. I am trying to activate and set a password for a specific user account. From my research, it seems like the process ends before anything can be transferred. I don't know why the background program net user
doesn't wait for input before exiting.
Here is the command I run programmatically to accomplish this:
net user username /active:yes & net user username *
The output of the second command looks like this:
Type a password for the user:
Retype the password to confirm:
The command completed successfully
If you ran the above command manually, it will ask you to enter a password and hide what you enter from the screen. However, the program doesn't seem to stop when programmed.
To call a program, I have a function that starts the program and returns the process to another function that sends input to the process. Here's the first function:
static Process RunCommandGetProcess(string command)
{
Process process = new Process();
ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.FileName = "CMD.exe";
psInfo.Arguments = "/C " + command + "& PAUSE";
// Allow for Input redirection
psInfo.UseShellExecute = false;
psInfo.RedirectStandardInput = true;
// Window style
psInfo.WindowStyle = ProcessWindowStyle.Normal;
// Start the mothertrucker!
process.StartInfo = psInfo;
process.Start();
return process;
}
And the call function:
static int ActivateUserWithPassword(string password)
{
// Start net user with that other function
Process process = RunCommandGetProcess("net user username /active:yes & net user username *");
StreamWriter streamWriter = process.StandardInput;
streamWriter.WriteLine(password); // First Prompt
streamWriter.WriteLine(password); // Second Prompt
process.WaitForExit();
return process.ExitCode;
}
However, when I run the debugger, the commands succeed until two lines streamWriter.WriteLine(password);
are executed! I tried Googling but didn't help.
You people are my only hope.
source to share
Good! I was very motivated to solve your problem as soon as I saw it! after 2 hours of non-stop debugging I have a solution for you!
Problem # 1: Your application does not have administrator privileges, so the command exits immediately after launch. Run the .exe with administrator privileges.
Problem # 2: Since the password is masked at the login, even if I did it streamWriter.WriteLine(password)
once, the logout is "successful". It is not possible to know if the password was actually passed or if an empty string was selected as the password.
Decision
You can use the command net user
with a parameter for the password like this
net user user_name password
. No need to prompt user for password. Ie Don't use '*' after the username since you are passing it through the program.
So this is how it works
Process proc = new Process();
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "cmd";
start.Arguments = "/k";
start.RedirectStandardInput = true;
start.WorkingDirectory = Environment.CurrentDirectory;
start.UseShellExecute = false;
proc.StartInfo = start;
proc.Start();
proc.StandardInput.WriteLine("net user \"username\" password");
Very important!
Run exe as administrator OR you will need to do this
start.UseShellExecute = true;
start.Verb = "runas";
But then you won't be able to redirect output / input streams !.
source to share