This question already has an answer here:
Run process as administrator from a non-admin application
(1 answer)
Closed 1 year ago.
I'm trying to run this command in cmd from c# but for some reason it doesn't work. But when I change my Argument to "/C ipconfig" it work perfectly. I don't understand why. What am I doing wrong?
Here is my code:
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = $"/C wbadmin start backup - backupTarget:{TargetBackupDrive}: -include:{LocationOs}: -allcritical - quiet";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.Verb = "runas";
process.Start();
CmdOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();
The verb "runas" does not work when UseShellExecute is set to false as it is pointed out here: Set ProcessStartInfo.EnvironmentVariables when Verb="runas"
You could either provide admin credentials with the UserName and Password properties our set UseShellExecute to true and execute your command in the shell.
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.
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);
To dump database from command line, all I need to do is:
mysqldump -uroot --password= myDb --routines> "C:\s.sql"
So all I would try programmatically is this, which is the direct interpretation of it I suppose:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = dumpUtilityPath;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.Arguments = "-uroot --password= myDb --routines> \"C:\\s.sql\"";
Process process = Process.Start(psi);
process.WaitForExit();
process.Close();
Which doesn't work at all. Instead I have to go for this which one can find all over the net, which works too.
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = dumpUtilityPath;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.Arguments = string.Format("-R -u{0} --password={1} -h{2} {3} --routines", "root", "", "localhost", "myDb");
Process process = Process.Start(psi);
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
using (StreamWriter writer = new StreamWriter("C:\\s.sql"))
{
writer.WriteLine(output);
writer.Close();
}
Why is that I need to use stream writer to get the database in an sql file which I can do otherwise directly from commands in command prompt?
What is the role of -R in the second block?
You can not redirect stdout using ">" in arguments because that is a feature of the command prompt.
-R includes the stored procedures and functions in the dump. See http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_routines for more information.
What you are doing in the command line version is using the shell to pipe the standard output to a file (the > command, followed by a file name, is a shorthand way of saying "take all of the standard output of this program and write it to this file"). To do the same thing from C#, you need to hand the standard output yourself and write it to a file.
The -R in the second example seems duplicative. According to this page, it is the same as --routines. Have you tried it without?
I thought I would include what the Arguments could look like programmatically, in our case we also wanted to dump the events of the DB to a file.
psi.Arguments = string.Format(#"-u{0} -p{1} -h{2} {3}", "someUser", "xxxxxx", "localhost", dbName, "--routines","--events");
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();