How to run in C# CMD with admin rights with password and then save all output to a string?
up vote
0
down vote
favorite
I would like to run "runas /user:Administrator C:Info.bat" in CMD. Admin user requires password which is ("pass"). When I confirm password I got data that I would like to save it into a string.
Here is my code:
// admin password with secure string
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";
//go to user -> Administrator and then to file C:\Info.bat (not working)
startInfo.Arguments = "/user:Administrator C:\Info.bat";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;
// save all output data to string
p.Start();
Why second argument not working to run C:Info.bat?
How can I save all cmd output text to a string?
Thanks for help.
c# batch-file cmd
|
show 5 more comments
up vote
0
down vote
favorite
I would like to run "runas /user:Administrator C:Info.bat" in CMD. Admin user requires password which is ("pass"). When I confirm password I got data that I would like to save it into a string.
Here is my code:
// admin password with secure string
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";
//go to user -> Administrator and then to file C:\Info.bat (not working)
startInfo.Arguments = "/user:Administrator C:\Info.bat";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;
// save all output data to string
p.Start();
Why second argument not working to run C:Info.bat?
How can I save all cmd output text to a string?
Thanks for help.
c# batch-file cmd
what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
– ADyson
Nov 19 at 11:58
1
try this and let me know =>startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
– er-shoaib
Nov 19 at 12:03
Thanks @er-shoaib that run C:Info.bat.
– Luka Toni
Nov 19 at 12:13
should i add above code as answer to your question?
– er-shoaib
Nov 19 at 12:27
1
@LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
– er-shoaib
Nov 19 at 13:57
|
show 5 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to run "runas /user:Administrator C:Info.bat" in CMD. Admin user requires password which is ("pass"). When I confirm password I got data that I would like to save it into a string.
Here is my code:
// admin password with secure string
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";
//go to user -> Administrator and then to file C:\Info.bat (not working)
startInfo.Arguments = "/user:Administrator C:\Info.bat";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;
// save all output data to string
p.Start();
Why second argument not working to run C:Info.bat?
How can I save all cmd output text to a string?
Thanks for help.
c# batch-file cmd
I would like to run "runas /user:Administrator C:Info.bat" in CMD. Admin user requires password which is ("pass"). When I confirm password I got data that I would like to save it into a string.
Here is my code:
// admin password with secure string
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";
//go to user -> Administrator and then to file C:\Info.bat (not working)
startInfo.Arguments = "/user:Administrator C:\Info.bat";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;
// save all output data to string
p.Start();
Why second argument not working to run C:Info.bat?
How can I save all cmd output text to a string?
Thanks for help.
c# batch-file cmd
c# batch-file cmd
edited Nov 19 at 13:09
asked Nov 19 at 11:40
Luka Toni
145313
145313
what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
– ADyson
Nov 19 at 11:58
1
try this and let me know =>startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
– er-shoaib
Nov 19 at 12:03
Thanks @er-shoaib that run C:Info.bat.
– Luka Toni
Nov 19 at 12:13
should i add above code as answer to your question?
– er-shoaib
Nov 19 at 12:27
1
@LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
– er-shoaib
Nov 19 at 13:57
|
show 5 more comments
what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
– ADyson
Nov 19 at 11:58
1
try this and let me know =>startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
– er-shoaib
Nov 19 at 12:03
Thanks @er-shoaib that run C:Info.bat.
– Luka Toni
Nov 19 at 12:13
should i add above code as answer to your question?
– er-shoaib
Nov 19 at 12:27
1
@LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
– er-shoaib
Nov 19 at 13:57
what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
– ADyson
Nov 19 at 11:58
what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
– ADyson
Nov 19 at 11:58
1
1
try this and let me know =>
startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
– er-shoaib
Nov 19 at 12:03
try this and let me know =>
startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
– er-shoaib
Nov 19 at 12:03
Thanks @er-shoaib that run C:Info.bat.
– Luka Toni
Nov 19 at 12:13
Thanks @er-shoaib that run C:Info.bat.
– Luka Toni
Nov 19 at 12:13
should i add above code as answer to your question?
– er-shoaib
Nov 19 at 12:27
should i add above code as answer to your question?
– er-shoaib
Nov 19 at 12:27
1
1
@LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
– er-shoaib
Nov 19 at 13:57
@LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
– er-shoaib
Nov 19 at 13:57
|
show 5 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You need to modify your process argument like below
startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
/K argument, which tells CMD.exe to open, run the specified command, then keep the window open.
You may also use.
/C argument, which tells CMD.exe to open, run the specified command, then close when it's done.
Edit:
Here you can read output of your info.bat
file in string variable.
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";
startInfo.Arguments = "/user:Administrator "cmd /C C:\info.bat"";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
p.StartInfo = startInfo;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You need to modify your process argument like below
startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
/K argument, which tells CMD.exe to open, run the specified command, then keep the window open.
You may also use.
/C argument, which tells CMD.exe to open, run the specified command, then close when it's done.
Edit:
Here you can read output of your info.bat
file in string variable.
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";
startInfo.Arguments = "/user:Administrator "cmd /C C:\info.bat"";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
p.StartInfo = startInfo;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
add a comment |
up vote
1
down vote
accepted
You need to modify your process argument like below
startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
/K argument, which tells CMD.exe to open, run the specified command, then keep the window open.
You may also use.
/C argument, which tells CMD.exe to open, run the specified command, then close when it's done.
Edit:
Here you can read output of your info.bat
file in string variable.
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";
startInfo.Arguments = "/user:Administrator "cmd /C C:\info.bat"";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
p.StartInfo = startInfo;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You need to modify your process argument like below
startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
/K argument, which tells CMD.exe to open, run the specified command, then keep the window open.
You may also use.
/C argument, which tells CMD.exe to open, run the specified command, then close when it's done.
Edit:
Here you can read output of your info.bat
file in string variable.
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";
startInfo.Arguments = "/user:Administrator "cmd /C C:\info.bat"";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
p.StartInfo = startInfo;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
You need to modify your process argument like below
startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
/K argument, which tells CMD.exe to open, run the specified command, then keep the window open.
You may also use.
/C argument, which tells CMD.exe to open, run the specified command, then close when it's done.
Edit:
Here you can read output of your info.bat
file in string variable.
var pass = new SecureString();
pass.AppendChar('p');
pass.AppendChar('a');
pass.AppendChar('s');
pass.AppendChar('s');
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("CMD");
startInfo.Verb = "runas";
startInfo.Arguments = "/user:Administrator "cmd /C C:\info.bat"";
startInfo.Password = pass;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
p.StartInfo = startInfo;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
edited Nov 20 at 5:39
answered Nov 19 at 12:35
er-shoaib
4,3092516
4,3092516
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373872%2fhow-to-run-in-c-sharp-cmd-with-admin-rights-with-password-and-then-save-all-outp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what does "not working" mean exactly? You get an error? Or some other unexpected behaviour?
– ADyson
Nov 19 at 11:58
1
try this and let me know =>
startInfo.Arguments = "/user:Administrator "cmd /K C:\Info.bat"";
– er-shoaib
Nov 19 at 12:03
Thanks @er-shoaib that run C:Info.bat.
– Luka Toni
Nov 19 at 12:13
should i add above code as answer to your question?
– er-shoaib
Nov 19 at 12:27
1
@LukaToni, i update my answer to read cmd data to string kindly view Edit section in answer :)
– er-shoaib
Nov 19 at 13:57