How I can call the installer.ini arguments in a silent installer? - c#

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();
}

Related

C# Process Argument is not working with startInfo.Arguments

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.

Deadlock in System.Diagnostic.Process

I have a small console application and I want to read the output in C#. Therefore I've created this code snippet. The command prompt opens, but nothing is displayed.
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
process.StartInfo.FileName = DirectoryPath + "Test.exe";
process.StartInfo.Arguments = "-showAll";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.WaitForExit(2000);
String strOutput = process.StandardOutput.ReadToEnd();
If I remove UseShellExecute, RedirectStandardOutput and the last line, the command prompt opens and the Test.exe is shown, but I need the output as String and so I have to use these attributes to read the StandardOutput
I've also tried to set a timeout of 2 seconds (process.WaitForExit(2000)), but the empty command prompt does not close after 2 seconds.
If I close the empty command prompt manually in debug mode, the variable strOutput has my requested information.
To avoid deadlock you have to read the output stream before you wait to exit. So try :
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
process.StartInfo.FileName = DirectoryPath + "Test.exe";
process.StartInfo.Arguments = "-showAll";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
String strOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();

c# Process exe with arguments gives error

I sort of stuck into what should be a small issue. I need to run two different processes but the second failures.
My first process is:
string workingDirectory = "c:\\xxxx\\bin\\";
string fileName = "xxxx.xxxx.cmd";
string arguments = "-Sxxxx -f -d --port xxxx > c:\\xxxx\\tmp\\bug.log 2>&1";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = workingDirectory;
startInfo.FileName = fileName;
startInfo.Arguments = arguments;
Process process = new Process();
process.StartInfo = startInfo;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
No worries here. Everything works but the second.
string workingDirectory = "c:\\xxxx\\bin\\";
string fileName = "WebDaemon.exe";
string arguments = "-debug -i WebDaemon.xxxx.1234 --ServerPort=4444 --MultipleServerPorts=1 --CGIPort=4500 --CGICallbackPort=4600 --MinServerProcs=2 --MaxServerProcs=2 > c:\\xxxx\\tmp\\WebDaemonTrace.log 2>&1";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = workingDirectory;
startInfo.FileName = fileName;
startInfo.Arguments = arguments;
Process process = new Process();
process.StartInfo = startInfo;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
Gives an error. The process created and shows up in taskmanager, but after 5 seconds it disappears. Furthermore i can read in a debug message that:
"commandline argument '>' is unknown"
I cannot seem to find the problem. Open to any suggestions :-)

Create a process that execute powershell in C# but failed outpt

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.

C# - Launch application with arguments.

Hello I've to Launch the software CFast for a Parametric Analysis. To do this, I want to create a application in C# that runs the core CFast.exe. If I want run the software from cmd.exe and execute it on the file INPUTFILENAME.in I write in prompt:
CFast.exe INPUTFILENAME
In C# I wrote the following code:
Process firstProc = new Process();
firstProc.StartInfo.FileName = #"C:\Users\Alberto\Desktop\Simulazioni Cfast\D\C\N\A3B1\CFAST.exe";
firstProc.StartInfo.Arguments = #"INPUTFILENAME";
firstProc.EnableRaisingEvents = true;
firstProc.Start();
firstProc.WaitForExit();
With this code CFast run but doesn't analyze anything... Seems like don't accept the argument. Hint for this trouble ?
Solved. Mistake in the filename and in the syntax of the command
// setup cmd process
var command = #"CFAST.exe C:\Users\Alberto\Desktop\Simulazioni_Cfast\D\C\N\A3B1\A3B1";
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardError = true;
procStartInfo.CreateNoWindow = true;
// start process
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
// read process output
string cmdError = proc.StandardError.ReadToEnd();
string cmdOutput = proc.StandardOutput.ReadToEnd();
where A3B1 is the name of the file .IN

Categories