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?
Related
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
I wonder if someone can please help me understand why my Process is not working, nor generating an error.
The code below should loop through a directory, find all files with an sqb extension and for each file run a Process as a user account which has elevated privileges on the server.
The process should run an executable sqb2mtf.exe from the same folder as the files are located with an argument such as sqb2mtf.exe file.sqb file.bak for example purposes.
If I use Visual Studio 2013 and step through the code I can see each file being looped through and the Process appears to fire, but no files are converted, nor any errors presented to the variable readToEndError.
var directory = new DirectoryInfo(#"D:\inetpub\Import\");
foreach (var file in directory .EnumerateFiles("*.sqb"))
{
var convert = Path.GetFileNameWithoutExtension(file.ToString());
var process = new Process
{
StartInfo =
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
FileName = #"D:\inetpub\Import\sqb2mtf.exe",
UserName = "myUserName",
Domain = "myDomain",
Password = GetSecureString("myPassword"),
Arguments = #"D:\inetpub\Import\" + file + " " + #"D:\inetpub\Import\" + convert + ".bak"
}
};
process.Start();
string readToEndOutput = process.StandardOutput.ReadToEnd();
string readToEndError = process.StandardError.ReadToEnd();
process.WaitForExit();
}
I am going out of my mind, any advice to resolve this would be much appreciated :-)
Update
var directoryInfo = new DirectoryInfo(BackupDirectory);
foreach (var file in directoryInfo.EnumerateFiles("*.sqb"))
{
var convert = Path.GetFileNameWithoutExtension(file.ToString());
var fileName = BackupDirectory + "sqb2mtf.exe";
var arguments = "\"" + BackupDirectory + file + "\" \"" + BackupDirectory + convert + ".bak\"";
var process = new Process
{
StartInfo =
{
CreateNoWindow = true,
UseShellExecute = true,
RedirectStandardOutput = false,
RedirectStandardInput = false,
RedirectStandardError = false,
FileName = fileName,
Arguments = arguments
}
};
process.Start();
process.WaitForExit();
file.Delete();
}
One thing drawing on from the comments by InBetween is the need for quotes, in this case the quotes needed to surround the two separate files.
I can confirm this code does work on IISExpress, impersonating the different user, unfortunately just not IIS 7.5.
A workround was to move this code into a Console Application and install on the server in question, then use a Windows Schedule Task to run as a specific account.
With some legacy apps, I've discovered that I need to pass the arguments as quoted text, otherwise they simply wouldn't work.
Not sure if this is the issue but it's worth the try:
Arguments = "\"D:\\inetpub\\Import\\" + file + " D:\\inetpub\\Import\\" + convert + ".bak\"";
Still it seems strange that the process would simply die silently. I'd double check Domain, UserName and Password.
Tearing my hair out over something that should be ridiculously simple! I have cd'd to C:\Program Files\ImageMagick-6.9.0-Q16 on my shell and used the following the command string which works on my machine; it creates the expected output image, no problem:
convert "C:\Users\someguy\Debug\test_in.jpg" -resize 75x75 -colorspace
RGB "C:\Users\someguy\Debug\test_out.jpg"
I am trying to automate this with a simple test application in C#:
var proc = new Process
{
StartInfo = new ProcessStartInfo()
{
//WorkingDirectory = #"C:\Program Files\ImageMagick-6.9.0-Q16\",
Arguments = //_arguments,
"convert \"" + InputPath + "\" -resize 75x75 \"" + OutputPath + "\""
,
UseShellExecute = _useShellExecute,
RedirectStandardError = _redirectStandardError,
//RedirectStandardOutput = _redirectStandardOutput,
//CreateNoWindow = _createNoWindow,
//Verb = _verb,
FileName = #"C:\Program Files\ImageMagick-6.9.0-Q16\" + "convert.exe"
}
};
var test = proc.StartInfo.Arguments.ToString();
proc.Start();
string error = proc.StandardError.ReadToEnd();
proc.WaitForExit();
I have tried several permutations of this, using convert.exe, mogrify.exe, with verb as "runas", with the working directory set on, or off... (see commented out stuff - I've tried setting it) I have referred to how to use imageMagick with C# but I continue to get the same error:
mogrify.exe: unable to open image convert': No such file or directory
# error/blob.c/OpenBlob/2709. mogrify.exe: no decode delegate for this
image format' # error/constitute.c/ReadImage/501. mogrify.exe:
unable to open image `C:\Users\someguy\Debug\test_out.jpg': No such
file or directory # error/blob.c/OpenBlob/2709.
I feel like I'm just missing something really basic here, but I don't have a clue at this point. Could someone please offer a suggestion?
var proc = new Process
{
StartInfo = new ProcessStartInfo(_imageMagickFile)
{
//WorkingDirectory = #"C:\Program Files\ImageMagick-6.9.0-Q16\",
Arguments = //_arguments,
"" + InputPath + " -resize 75x75 " + OutputPath + ""
,
UseShellExecute = _useShellExecute,
RedirectStandardError = _redirectStandardError,
RedirectStandardOutput = _redirectStandardOutput,
CreateNoWindow = _createNoWindow,
Verb = _verb,
FileName = _imageMagickFile
}
};
var test = proc.StartInfo.Arguments.ToString();
proc.Start();
string error = proc.StandardError.ReadToEnd();
proc.WaitForExit();
I'm not actually sure why this works instead of the original question, but it does. Using convert.exe is indeed correct, not mogrify.exe. As an FYI, the other parameters are ShellExecute = false, Redirect std err/out are set to true, verb is runas.
I'd like to use C# to execute a shell script.
Based on similar questions I came to a solution that looks like this.
System.Diagnostics.Process.Start("/Applications/Utilities/Terminal.app","sunflow/sunflow.sh");
It currently opens Terminal, then opens the shell file with the default application (Xcode in my case). Changing the default application is not an option, since this app will need to be installed for other users.
Ideally the solution will allow for arguments for the shell file.
I can't test with a Mac right now, but the following code works on Linux and should work on a Mac because Mono hews pretty closely to Microsoft's core .NET interfaces:
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "foo/bar.sh",
Arguments = "arg1 arg2 arg3",
};
Process proc = new Process()
{
StartInfo = startInfo,
};
proc.Start();
A few notes about my environment:
I created a test directory specifically to double-check this code.
I created a file bar.sh in subdirectory foo, with the following code:
#!/bin/sh
for arg in $*
do
echo $arg
done
I wrapped a Main method around the C# code above in Test.cs, and compiled with dmcs Test.cs, and executed with mono Test.exe.
The final output is "arg1 arg2 arg3", with the three tokens separated by newlines
Thanks Adam, it is good starting point for me. However, for some reason when I tried with above code (changed to my needs) I am getting below error
System.ComponentModel.Win32Exception: Exec format error
see below code that gives above error
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "/Users/devpc/mytest.sh",
Arguments = string.Format("{0} {1} {2} {3} {4}", "testarg1", "testarg2", "testarg3", "testarg3", "testarg4"),
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
Process proc = new Process()
{
StartInfo = startInfo,
};
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
string result = proc.StandardOutput.ReadLine();
//do something here
}
and spent some time and come up with below and it is working in my case - just in case if anyone encounter this error try below
Working Solution:
var command = "sh";
var scriptFile = "/Users/devpc/mytest.sh";//Path to shell script file
var arguments = string.Format("{0} {1} {2} {3} {4}", "testarg1", "testarg2", "testarg3", "testarg3", "testarg4");
var processInfo = new ProcessStartInfo()
{
FileName = command,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
Process process = Process.Start(processInfo); // Start that process.
while (!process.StandardOutput.EndOfStream)
{
string result = process.StandardOutput.ReadLine();
// do something here
}
process.WaitForExit();
When I open a command window in windows and use the imagemagick convert command:
convert /somedir/Garden.jpg /somedir/Garden.png
It works as expected.
What I am trying to do is executing the same command as above using C#.
I tried using System.Diagnostics.Process, however, no foo.png gets created.
I am using this code:
var proc = new Process
{
StartInfo =
{
Arguments = string.Format("{0}Garden.jpg {1}Garden.png",
TEST_FILE_DIR,
TEST_FILE_DIR),
FileName = #"C:\xampp\ImageMagick-6.5.4-Q16\convert",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = false,
RedirectStandardError = false
}
};
proc.Start();
No exception gets thrown, but no .png is written, either.
My guess is that TEST_FILE_DIR contains a space - so you have to quote it.
Try this:
Arguments = string.Format("\"{0}Garden.jpg\" \"{1}Garden.png\"",
TEST_FILE_DIR,
TEST_FILE_DIR)
You might also want to give the filename including the extension, e.g.
FileName = #"C:\xampp\ImageMagick-6.5.4-Q16\convert.exe"