Main service stopping himself after running function that stops another service - c#

I have a C# service called Dlocker, Dlocker has another service called DlockerUpdate, that updates Dlocker.
I'm making a function to Dlocker called checkUpdaterUpdates() that will check if a file called DlockerUpdate.exe.new exists in the DlockerUpdate folder, if exists, it will follow these steps:
Stop the service domtoolsupdate (DlockerUpdate.exe)
Remove the old DlockerUpdate.exe file
Rename DlockerUpdate.exe.new to DlockerUpdate.exe
Start the service
Here is the code for the StopService(), StartService() and checkUpdaterUpdates():
private void StartService()
{
try
{
helper.WriteToFile("{0} inside startService");
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.Verb = "runas";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C net start domtoolsupdate";
process.StartInfo = startInfo;
process.Start();
}
catch (Exception error)
{
helper.WriteToFile("{0} error: " + error);
}
helper.WriteToFile("{0} DlockerUpdate Started");
}
private void StopService()
{
try
{
helper.WriteToFile("{0} inside stopService");
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.Verb = "runas";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C net stop domtoolsupdate";
process.StartInfo = startInfo;
process.Start();
}
catch (Exception error)
{
helper.WriteToFile("{0} error: " + error);
}
helper.WriteToFile("{0} DlockerUpdate Stopped");
}
private void checkUpdaterUpdates()
{
var DlockerUpdatePath = AppDomain.CurrentDomain.BaseDirectory + "..\\update\\";
if (File.Exists(DlockerUpdatePath + "DlockerUpdate.exe.new")) {
helper.WriteToFile("in the if");
StopService();
File.Delete(DlockerUpdatePath + "DlockerUpdate.exe");
helper.WriteToFile("deleted dlockerupdate.exe");
File.Move(DlockerUpdatePath + "DlockerUpdate.exe.new", DlockerUpdatePath + "DlockerUpdate.exe");
helper.WriteToFile("updated dlockerupdate.exe");
StartService();
helper.WriteToFile("started the service");
}
}
In the StopService(), instead of stopping only domtoolsupdate (DlockerUpdate.exe), it's stopping domtools (Dlocker.exe) and domtoolsupdate, and I can't find out why.
I've copied and adapted the start and stop service functions from DlockerUpdate.exe to work on Dlocker.exe, but it doesn't works for some reason.
As you have seen, in the code there is a function called helper, the helper is my debugger, when I call him, it writes on a file called Dlocker.log, when the stop service is running, this is the result of the helper:
The log does not return any signals of Dlocker.exe has stopped, but in the log of DlockerUpdate.exe, it returns that the service has been stopped:
There's no function in DlockerUpdate.exe that kills Dlocker.exe when it's stopped.
What should I try?

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.

Return the result while the process is running

I have a program which start a batch process. This batch file is a consuming process and I want to return the result while the process is running. I try to use await and async but nothing works.
result = String.Empty;
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = RunText;
startinfo.Arguments = "cmd";
Process process = new Process();
process.StartInfo = startinfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
while (!process.StandardOutput.EndOfStream)
{
result = process.StandardOutput.ReadLine();//I want to return the result
Console.WriteLine(result);
}
-This is my batch program. The output is 1 if there is a connection with another PC.
#echo off
:begin
for /F "tokens=*" %%L in ('netstat -n ^| find ":3389" ^| find "ESTABLISHED" /c') do (set "VAR=%%L")
echo %VAR%
goto begin
I am beginner with this topics(processes and multi-threading). Any help would be greatly appreciated.
Use handlers to achieve this:
public static void MyHandler(object process, DataReceivedEventArgs dataReceived)
{
if (!String.IsNullOrEmpty(dataReceived.Data))
{
// do something you want here, for an example
Console.WriteLine(dataReceived.Data);
}
}
And in main method:
//...
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler(MyHandler);
process.Start();

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.

cmd command don't execute in C#

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

process.startInfo fails on windows 2012 64-bit

i have a small application to configure IIS using Process.StartInfo. The application works fine in Windows 2008 R2 64-bit and all the 32-bit machines. I dont see much differensce even if i use Runas. The application is run as local admin on the box.
the code is below
namespace ConsoleApplication1
{
class Program
{
private const string configIISParameters2012 = "/k START /w C:\\Windows\\SysNative\\PKGMGR.EXE /iu:IIS-WebServerRole;IIS-WebServer;IIS-CommonHttpFeatures;IIS-StaticContent;
private const string exeCommand = "CMD.EXE";
static void Main()
{
RunCommand(exeCommand, configIISParameters2012);
}
public static bool RunCommand(String command, String arguments)
{
bool ret = false;
try
{
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.Verb = "unas";
startInfo.FileName = command;
startInfo.Arguments = arguments;
process.StartInfo = startInfo;
process.StartInfo.UseShellExecute = true;
process.EnableRaisingEvents = true;
process.Start();
process.WaitForExit();
process.Close();
ret = true;
}
catch (Exception ex)
{
//Utility.Log("Exception executing command :" + ex.StackTrace);
}
return ret;
}

Categories