I dont know where is my missing. Even it dit not write out log. Could you fix my test code:
private void button1_Click(object sender, EventArgs e)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
startInfo.Arguments = #"/C Import-Module AppLocker;Set-AppLockerPolicy -XMLPolicy 'iPolicy.xml' > C:\log.txt";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
//startInfo.RedirectStandardOutput = true;
//startInfo.RedirectStandardError = true;
//startInfo.UseShellExecute = false;
//startInfo.CreateNoWindow = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
catch (Exception ex) {
Debug.Print("Error: " + ex.Message);
}
}
Finally got it to work, code should look like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"cmd.exe";
startInfo.Arguments = #"/C powershell.exe /C Import-Module AppLocker;Set-AppLockerPolicy -XMLPolicy 'iPolicy.xml' > C:\log.txt";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
This worked for me! You can use full paths to cmd.exe and powershell.exe if you feel necessary, it's not that important here.
Related
I am doing an unattended installer, I ran it with cmd as below, and it is working without any problem.
setup.exe -q -J -Djava.security.manager=allow
Now I am trying to use the same arguments in my c# console application code, but it will ignore the arguments. Can you please check the method below and support if there is a way to do it?
static int RunSetup(out string stdout)
{
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.Arguments = "-q -J -Djava.security.manager=allow";
startInfo.FileName = "setup.exe";
startInfo.WorkingDirectory = Environment.CurrentDirectory;
process.StartInfo = startInfo;
process.Start();
stdout = process.StandardOutput.ReadToEnd();
process.WaitForExit();
int exitCode = process.ExitCode;
process.Close();
return exitCode;
}
catch (Exception exception)
{
throw new Exception("exeption = " + exception.Message);
}
}
I wrote a small .net console application that just prints the arguments that are passed to the application:
Console.WriteLine(string.Join(":", args ));
and named it setup.exe.
When i used your code example to call it it prints out -q:-J:-Djava.security.manager=allow as expected, so your code seem to work just as you expected. This was tested using .NET 7, though.
I want to run cmd command with C# for install service in Windows, I'm using of below code:
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.Arguments =
"\"C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe\" \"D:\\Projects\\MyNewService\\bin\\Release\\MyNewService.exe\"";
process.StartInfo = startInfo;
process.Start();
}
}
but this program don't work. if I run this command in cmd.exe work properly but when I run this project don't execute command and MyNewService.exe don't install.
where is my problem?
do you help me?
Instead of starting a cmd.exe and passing installutil as an argument (then your service executabel an argument of the argument), try starting the installutil.exe executable directly passing the MyNewService.exe as the argument.
You should always wait for the process to exit, and should always check the exit code of the process as below.
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\installutil.exe";
startInfo.Arguments = "D:\\Projects\\MyNewService\\bin\\Release\\MyNewService.exe";
process.StartInfo = startInfo;
bool processStarted = process.Start();
process.WaitForExit();
int resultCode = process.ExitCode;
if (resultCode != 0)
{
Console.WriteLine("The process intallutil.exe exited with code {0}", resultCode);
}
}
I use this code to install in silent mode a program :
private void Install_Click(object sender, EventArgs e)
{
string path = AppDomain.CurrentDomain.BaseDirectory.ToString();
string configfilename = path + "config.ini";
string installerfilename = path + "installer.ini";
string configtext = File.ReadAllText(configfilename);
string installertext = File.ReadAllText(installerfilename);
}
Process process = new Process();
process.StartInfo.FileName = #"D:\Matlab\NSIS\R2008a\win64\setup.exe";
process.StartInfo.Arguments = "/quiet";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
}
But this code made setup.exe to run, but doesn't use the installer.ini where I have the license number, outlog number ...How I can do this, to use the arguments of installer.ini for a silent installer of Matlab program ?
Also I tried this :
Process process = new Process();
process.StartInfo.FileName = #"D:\Matlab\NSIS\R2008a\win64\setup.exe";
process.StartInfo.Arguments = #"C:\temp\installer.ini";
process.StartInfo.Arguments = "/quiet";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
As far as I understood
http://www.mathworks.com/matlabcentral/answers/106140-how-do-i-utilize-silent-activation-using-activate-ini
http://www.mathworks.com/help/install/ug/install-noninteractively-silent-installation.html
the installer wants -if key and installer.ini file, so the C# syntax will be
// Put IDisposable into using
using (Process process = new Process()) {
// The installer itself
process.StartInfo.FileName = #"D:\Matlab\NSIS\R2008a\win64\setup.exe";
// suggested (see links above) arguments
process.StartInfo.Arguments = #"-if C:\temp\installer.ini";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
}
I try to start DoxBox from my console application, but it simply do nothing. What can be a problem?
Process proc = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.UseShellExecute = false;
startinfo.WorkingDirectory = "C:\\Program Files (x86)\\DoxBox";
startinfo.FileName = "cmd.exe";
string strComm = "DoxBox.exe /dismount H:";
startinfo.Arguments = strComm;
proc.StartInfo = startinfo;
proc.Start();
Why do you start the procces with cmd.exe?
It isn't in your C:\\Program Files (x86)\\DoxBox directory, I'm sure.
Provide the DoxBox file in the FileName, without cmd.exe at all:
Process proc = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.UseShellExecute = false;
startinfo.WorkingDirectory = "C:\\Program Files (x86)\\DoxBox";
startinfo.FileName = "DoxBox.exe";
string strComm = "/dismount H:";
startinfo.Arguments = strComm;
proc.StartInfo = startinfo;
proc.Start();
After looking on forums, I have written this snippet:
public string ExecuteCmd()
{
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 = this.m_command;
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
The m_command is member of a class, initialized in the constructor.
For my tests, it is net user. When the compiler arrives at this point, I get the following exception:
StandardOut has not been redirected or the process hasn't started yet.
Where is my mistake?
You need this:
//....
startInfo.Arguments = "/C " + this.m_command;
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
//....