I'm writing a program which creates a PDF at various intervals of time. I'm trying to run a PHP command to generate the PDF using a windows service (C#).
My code works just fine from within a C# windows desktop app, but when I try to use it in a windows service, nothing happens and the exit line appears in output without generating a PDF file.
Here is my code:
ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
cmdStartInfo.FileName = #"cmd.exe";
cmdStartInfo.RedirectStandardOutput = true;
cmdStartInfo.RedirectStandardError = true;
cmdStartInfo.RedirectStandardInput = true;
cmdStartInfo.UseShellExecute = false;
cmdStartInfo.CreateNoWindow = true;
cmdStartInfo.ErrorDialog = false;
cmdStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmdStartInfo.WorkingDirectory = WorkingDirectory; //refers to directory where php file exist.
Process cmdProcess = new Process();
cmdProcess.StartInfo = cmdStartInfo;
cmdProcess.ErrorDataReceived += cmdProcess_ErrorDataReceived;
cmdProcess.OutputDataReceived += cmdProcess_OutputDataReceived;
cmdProcess.EnableRaisingEvents = true;
cmdProcess.Start();
cmdProcess.BeginOutputReadLine();
cmdProcess.BeginErrorReadLine();
cmdProcess.StandardInput.WriteLine(#"php.exe -c GeneratePDF.php ");
cmdProcess.StandardInput.WriteLine("Exit");
Related
I am trying to read output from other process at real time line by line. But it showing output after process stop/closed. I am not sure what is wrong. I am trying to execute python exe. Here is my code:
var info = new ProcessStartInfo();
info.FileName = "main.exe";
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.UseShellExecute = false;
Process other = new Process();
other.StartInfo = info;
other.OutputDataReceived += new DataReceivedEventHandler(NetOutputDataHandler);
other.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler);
other.Start();
other.BeginOutputReadLine();
other.WaitForExit();
I wrote a simple TCP Server application,
when the program gets the request it will call an exe file to do something.
The application works fine on my PC.
But when I run on another PC the thread will stuck because it stuck in process start, so I checked the task manager whether the exe file did run. and it really work.
I have no idea how to solve it.
Here is my code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
string path = System.Environment.CurrentDirectory;
process.StartInfo.WorkingDirectory = path;
process.StartInfo.FileName = "FileTransferTool\\FileTransfer.exe";
process.StartInfo.Arguments = argument;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = false;
process.Start(); // the thread stuck here
I print out the message, it shows
System.ComponentModel.Win32Exception (0x80004005):
The system cannot find the file specified at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startIn
fo)
at System.Diagnostics.Process.Start() at ClassPackage.DsUploadFiletoNC.Start(Object[] param)
I have solved this problem by adding another process to call cmd.exe on the top of the main program, the code shown as below :
And the program will run as Administrator, but I don't know why
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
string path = System.Environment.CurrentDirectory;
process.StartInfo.WorkingDirectory = #"C:\Windows\System32";
process.StartInfo.FileName = "cmd.exe";
//process.StartInfo.FileName = "FileTransferTool\\circle.txt";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardInput = true;
//process.StartInfo.CreateNoWindow = false;
process.Start();
using (TCPServer server = new TCPServer())
{
server.Start();
Console.WriteLine("Socket Server Start...");
Console.ReadKey(true);
}
}
I'm trying to run an impersonated R-Session from .net Core. Here is the code i'm running:
info.WorkingDirectory = #"C:\r_installs\R-3.3.2\bin\x64";
info.FileName = #"C:\r_installs\R-3.3.2\bin\x64\R.exe";
info.Arguments = $"--vanilla";
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.UseShellExecute = false;
info.CreateNoWindow = true;
info.ErrorDialog = true;
info.Domain = _Cred.Domain;
info.UserName = _Cred.UserName;
info.Password = _Cred.PwdSecure;
var proc = new Process();
proc.OutputDataReceived += new DataReceivedEventHandler(ReadAsyncOutput);
proc.ErrorDataReceived += new DataReceivedEventHandler(ReadAsyncOutput);
proc.StartInfo = info;
proc.Start();
proc.WaitForExit();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
When the Code is getting executed everthing seems fine, but after about 20 seconds the R.exe exits and the Eventlog writes out an Applicationerror (0xc0000142).
When I tested it on my local client everything works fine. But after the deployment to the server, I got the described behaviour.
My local client runs Win10 x64
The remote server runs Windows Server 2016
I am using a windows application to start a console application for command line parameters configuration.
When I am sending the command line parameters through debug mode, the application is working perfect, and all Console.WriteLine is printing to console, but when am starting the process from windows application of that console application it is not showing console output
the way, am starting the process is
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.FileName = EXEName;
procStartInfo.Arguments = FilePath;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
using (Process process = new Process())
{
process.StartInfo = procStartInfo;
process.Start();
}
You need to set ProcessStartInfo.RedirectStandardOutput to false;
I have a very simple batch file:
echo Text > Test.txt
This file is saved here:
R:\Testing123.bat
full UNC pathway is
\\imfile\depart$\DB\Testing123.bat
In my console application the following runs:
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.WorkingDirectory = #"R:\";
myProcess.StartInfo.FileName = #"Testing123.bat";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
This does not run if I use the full pathway:
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true ;
myProcess.StartInfo.WorkingDirectory = #"\\imfile\depart$\DB\";
myProcess.StartInfo.FileName = #"Testing123.bat";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
Can I not use these UNC pathways for the WorkingDirectory property? I thought when programming it was always best practice to use these pathways?
EDIT
Using one of the suggestions I now have the following which unformtunately still doesn't work :
{
Process myP = new Process();
myP.StartInfo.UseShellExecute = false;
string myString = #"pushd \\imfile\depart$\DB";
myP.StartInfo.FileName = "cmd";
myP.StartInfo.Arguments = myString;
myP.StartInfo.CreateNoWindow = true;
myP.Start();
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true ;
myProcess.StartInfo.WorkingDirectory = #"\\imfile\depart$\DB";
//myProcess.StartInfo.WorkingDirectory = #"R:\";
myProcess.StartInfo.FileName = #"Testing123.bat";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
bat-files are executed via cmd.exe, which (by default) does not allows UNC paths as "working directory". However, you may change this behavoir via registry.
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"DisableUNCCheck"=dword:00000001
http://support.microsoft.com/kb/156276
This information is known to me from Far Manager TechInfo Section 2.2.