Error while executing binary file - c#

Ok, hello there. I have some code which should execute my binary file and print all output:
Process Program = new Process();
Program.StartInfo.FileName = "file.bin";
Program.StartInfo.WorkingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "/Build." + this.name;
Program.StartInfo.RedirectStandardOutput = true;
Program.StartInfo.UseShellExecute = false;
Program.Start();
string output = Program.StandardOutput.ReadToEnd();
Program.WaitForExit(1000);
Console.Out.WriteLine(output);
But when i ran it i get this error:
At the screenshot you can see file name and full path. Ok, we go in console:
Hey! But that file exists! I already tried that with relative path. Still not working.
P.S. Mono, Ubuntu 14.04
P.P.S. When i remove UseShellExecute = false my file is opening in gedit.
P.P.P.S. File is 100% exists:
var fi = new FileInfo(Path.Combine("Build." + this.name, "file.bin"));
Console.Out.WriteLine(fi.Exists); //true

From the documentation for ProcessStartInfo.UseShellExecute
"When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used only by the process that is started and has meaning only within the context of the new process. When UseShellExecute is false, the FileName property must be a fully qualified path to the executable."

Related

Execute batch file duplicate file name exists, or the file cannot be found

Executing by C# a complicated batch file, that setting session variables example
SET TEST = rainbow
getting the famous
A duplicate file name exists, or the file cannot be found.
I used
string args = string.Format("/k \"cd /d {0} && {1}\"", s.Path, s.Filename + " " + userChoice);
RunBatch("cmd.exe", args, s.Path);
.
.
ProcessStartInfo startInfo = new ProcessStartInfo()
{
UseShellExecute = false,
WorkingDirectory = workingDir,
FileName = cmd,
Arguments = cmdArgs
};
Process.Start(startInfo);
when trying it on simple batch file working. With the complex one getting the error mention above.
--
I tried also to write by C#, a new batch file that has on the first line the
setlocal enableextensions disabledelayedexpansion
, then calling the needed one, and execute this batch by C#, the error again is the same...
any tip?

Run executable in a silent mode

I am executing .exe file in C# using the code below.
If I want to run executable in a silent mode I usually uncomment UseShellExecute and RedirectStandardOutput properties, but this gives me an error:
Unhandled Exception: System.ComponentModel.Win32Exception: The system cannot find the file specified
If I keep a code like this it runs, but the additional command line screen is popping up and closing.
I am running Poisson Surface Reconstruction .exe and wondering if the silent mode is possible or not? Or it must be implemented by author who did this executable?
var proc = new System.Diagnostics.Process {
StartInfo = new System.Diagnostics.ProcessStartInfo {
FileName = "PoissonRecon",
Arguments = "--in " + fileNameIn + " --out " + fileName + " --depth "+depth.ToString()+" --pointWeight 0 --colors",
//UseShellExecute = false,
//RedirectStandardOutput = true,
CreateNoWindow = true,
WorkingDirectory = filePath
}
};
proc.Start();
proc.WaitForExit();
Try adding to arguments line this:
"--mode unattended"
if I'm not mistaken it should make installation silent

Get and Set the position in Process.StandardOutput

See as an example I open CMD and navigate to the folder I want to get the data from then I use it to open the app with arugents with standard input(all synchronously) the code so far
public static Process Start(bool DoNotShowWindow = false)
{
ProcessStartInfo cmdStartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = DoNotShowWindow
};
Process cmdProcess = new Process
{
StartInfo = cmdStartInfo,
EnableRaisingEvents = true
};
return cmdProcess;
}
//in other method
Process cli = InteractWithConsoleApp.Start();
cli.Start();
cli.StandardInput.WriteLine("cd /");
cli.StandardInput.WriteLine("cd " + path);
cli.StandardInput.WriteLine("fantasygold-cli getbalance abc");
Thread.Sleep(5000);
Problem
Now when I use StandardOutput.Readline, it starts from the beginning and returns me everything like first two lines of copyright,empty lines and even the input which in my case, after waiting 5 secs for the result I want to read line or to the end depending the input from where I had inputted.
possible solution
One solution I found was to change the position but it turned out it doesn't support it and even copying to another stream reader doesn't works(the position is not by line).
Well I can use filters like check a double or for an address starts with F and has a length of 36. The problem comes when I want to get the whole JSON say for like the past transactions, for which I think using filters like '{' and then check for '}'Caveat in this would be bad code, which I don't want.
TLDR
So, what could be the solution to my problem here :)
I found the answer, to open the file in subdirectory just use this cli.StartInfo.FileName = Directory.GetCurrentDirectory() + #"\SubfolderName\fantasygold-cli";
and the arguements like getbalance as cli.StartInfo.Arguments = "getbalance amunim"

Trying to transpile a js file in wpf c# project

I'm attempting to call Process.Start to invoke Babel and transpile a js file in my c# project.
I've installed babel into a directory "ES6" using the command:
npm install babel-preset-es2015 --save-dev
the directory C:\ES6\node_modules.bin now has a babel and babel.cmd file
Now I'm attempting to transpile a js file using Process.Start and redirecting the std out to capture the results I can use:
string babelFileName = #"C:\ES6\node_modules\.bin\babel";
var startInfo = new ProcessStartInfo {
FileName = babelFileName,
Arguments = " --presets es2015 " + ViewModel.EditorViewModel.JSFullFilePath,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
string js = ViewModel.EditorViewModel.Javascript;
using (var process = Process.Start(startInfo)) {
var standardOutput = new StringBuilder();
// read chunk-wise while process is running.
while (!process.HasExited) {
standardOutput.Append(process.StandardOutput.ReadToEnd());
}
int exitCode = process.ExitCode;
// make sure not to miss out on any remaindings.
standardOutput.Append(process.StandardOutput.ReadToEnd());
string stdout = standardOutput.ToString();
js = string.IsNullOrEmpty(stdout) ? js : stdout;
}
I've tried to invoke bable using "bable." to get around the missing ".exe" extension but no luck. I get the following exception:
The specified executable is not a valid application for this OS platform.
Hoping someone can point out how I can properly invoke babel to do this.
**UPDATE
I took a look at babel.cmd since this command line does transpile correctly:
C:\ES6\node_modules.bin>babel someES5.js --presets es2015
babel.cmd:
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\..\babel-cli\bin\babel.js" %*
) ELSE (
#SETLOCAL
#SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\..\babel-cli\bin\babel.js" %*
)
And modified my C#:
string babeljs = #"C:\ES6\node_modules\babel-cli\bin\babel.js";
var startInfo = new ProcessStartInfo {
FileName = "node.exe",
Arguments = babeljs + " " + ViewModel.EditorViewModel.JSFullFilePath,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
This does essentially echo the un-transpiled js code verbatim. If I now add " --presets es2015 " to the arguments, the process completes successfully but with empty output.
What do I need to add here to get transpiled js from this node.exe process?

Launching a batch file

I have the following code:
String Antcbatchpath = #"C:\GUI\antc.bat";
System.Diagnostics.Process runantc = new System.Diagnostics.Process();
runantc.StartInfo.FileName = Antcbatchpath;
runantc.StartInfo.UseShellExecute = false;
runantc.StartInfo.RedirectStandardOutput = true;
runantc.StartInfo.RedirectStandardError = true;
runantc.Start();
Will this load the batch file from C:\GUI\antc.bat?
Or runantc.StartInfo.FileName is only for a root directory? Root directory is where the application is located
EDIT 1:
hi instead of #"C:\GUI\antc.bat" i have a path:
String Antcbatchpath =#"C:\GUI Lab Tools\Build Machine\antc.bat";
which essentially contains white spaces. will it affect the runantc.StartInfo.Filename = Antcbatchpath; ?
UseShellExecute = true should do it.
Alternatively, if you need redirection, use:
runantc.StartInfo.FileName = "CMD.EXE";
runantc.StartInfo.Arguments = "/C " + Antcbatchpath;
You can try to set WorkingDirectory to prevent any ambiguity, but in my experience, it is not necessary.
The problem you're having is because antc.bat is not an executable. It requires UseShellExecute to be true, but that would prevent you from redirecting the output. I guess you will have to choose either one.

Categories

Resources