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);
}
}
Related
I am trying to call an executable from a webapi. When I run the code in Visual Studio it works perfect, but when I host it on a test server on IIS it doesn't work. Also I don't get any errors. What am I missing here.? Other functions which doesn't require that executable works fine.
Here is my code.
string output = "";
ProcessStartInfo startinfo = new ProcessStartInfo();
var path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "ExeDir");
var err = "";
startinfo.FileName = path + #"\Executable.exe";
Process process = new Process();
process.StartInfo = startinfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
try
{
process.Start();
}
catch(Exception e)
{
err = e.Message;
}
I'm developing a C# application and I need to run an external console process (for example a python script) and receive the output data of the script in real-time. The python script is something like this:
import time
while 1:
print("Hi Foo!")
time.sleep(.3)
The following C# code prints the python script output on the C# console in real-time:
static void Main(string[] args)
{
using (Process process = new Process())
{
process.StartInfo.FileName = "python.exe";
process.StartInfo.Arguments = "test.py";
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
}
}
However, when I try to capture the output data and write them on the console manually, I fail. The recommended solution according to other posts is something like this, but it doesn't work:
static void Main(string[] args)
{
using (Process process = new Process())
{
process.StartInfo.FileName = "python.exe";
process.StartInfo.Arguments = "test.py";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
}
}
The process.StandardOutput.ReadToEnd() works in blocking mode waiting for the process to exit and returns the whole output all at once. What exactly is the problem with the real-time output capturing and how can I fix it?
using (var process = new Process())
{
process.StartInfo.FileName = #"python.exe";
process.StartInfo.Arguments = "-u test.py";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
while (!process.StandardOutput.EndOfStream)
{
string line = process.StandardOutput.ReadLine();
Console.WriteLine(line);
// do something with line
}
process.WaitForExit();
Console.Read();
}
I am having trouble with the Process class to pipe a command on a Linux system.
I want to execute the following command: rpm2cpio repo.rpm | cpio -divm
I've tried
process.StartInfo.FileName = "rpm2cpio;
rocess.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.Arguments = "repo.rpm | cpio - idmv";
But the program hangs.
Similarly, I tried saving the output from rpm2cpio to a string or an output file and then pass that as the argument for the cpio command, but it also hangs.
process.StartInfo.FileName = "cpio";
rocess.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.Arguments = "-idvm < output.txt";
// or
process.StartInfo.Arguments = "-idvm < " + rp2cpio_output;
What are some ways I can get this working? I saw this post with a solution, but it is on a Window's system. How do the same thing on Linux?
Setting process.StartInfo.RedirectStandardOutput=true will cause the program to redirect standard output to the stream process.StartInfo.StandardOutput. When this happens the program will hang until you read from standard output.
To get the behavior I think you are looking for, you just need to set RedirectStandardOutput=false. That way the pipes and redirects in your command will work as expected.
Rather than directly writing to a file, you can simply use a StreamWriter to fetch the output in a stream buffer and then use that to write to the file. If the process still hangs, simply use the timeout command of linux to terminate the process.
The following snippet may help after making a few changes:
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.FileName = "/bin/bash";
processStartInfo.WorkingDirectory = "/";
string cmd = "timeout 1 cat > temp.txt";
var escapedArgs = cmd.Replace("\"", "\\\"");
processStartInfo.Arguments = $"-c \"{escapedArgs}\"";
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;
processStartInfo.StandardInputEncoding = System.Text.Encoding.UTF8;
processStartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
processStartInfo.UseShellExecute = false;
process.StartInfo = processStartInfo;
process.Start();
stdIOWriter = process.StandardInput;
stdIOWriter.WriteLine("Hey Fellas");
String error = process.StandardError.ReadToEnd();
String output = process.StandardOutput.ReadToEnd(); ```
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.