Reputation: 459
i have 1 c# console appln,it executes any script file by using Process.Start() method. i provide script file path to Process1.StartInfo.FileName
.My script file can be of any type (.vbs ,ps1 etc) . I m also passing String to script by using instruction p.StartInfo.Arguments
. When script file executed it should retrun string back to c# application. This returned string can be read by setting Process1.StartInfo.RedirectStandardOutput = true
,But for using this instruction i need to set Process1.StartInfo.UseShellExecute = false
.
When i run this i m getting error as "The specified executable is not a valid Win32 application".
i think this may be because of, when i set Process1.StartInfo.UseShellExecute = false
, my appln dont know which .exe to be used to execute script file.
on the other hand if i provide exe path to StartInfo.FileName
and script file path to StartInfo.Argument
then i m not getting error.
For Example:i want to execute powershell script and i set following properties as P1.StartInfo.FileName = "location of powershell.exe"
and p1.startInfo.Argument =".ps1 script file path"
, in this case i m not getting error.
problem is i dont know in advance,which type of script i m going to execute.also cant find out locations of .exe file for executing script file on different diffrent m/c. So is it possible to execute diffrent type of script files from same common c# appln and also read output returned by scripts?
here is my code
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;
namespace process_csharp
{
class Program
{
static void Main(string[] args)
{
String path = null;
//this will read script file path
path = Console.ReadLine();
//this string is passed as argument to script
string s = "xyz";
Process p = new Process();
p.StartInfo.FileName= path;
p.StartInfo.Arguments = s;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.BeginOutputReadLine();
Console.WriteLine(p.StandardOutput.ReadToEnd());
}
}
}
Upvotes: 0
Views: 3164
Reputation: 1212
You can check for the script type and read output from their own engine;
static void Main(string[] args)
{
string path = Console.ReadLine();
string parameter = Console.ReadLine();
string enginePath;
switch (Path.GetExtension(path).ToLowerInvariant())
{
case ".ps1":
enginePath = "powershell.exe";
break;
case ".vbs":
enginePath = "cscript.exe";
break;
default:
throw new ApplicationException("Unknown script type");
}
string scriptPath = path;
Process process = new Process();
process.StartInfo.FileName = enginePath;
process.StartInfo.Arguments = string.Format("{0} {1}", scriptPath, parameter);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
Console.WriteLine(process.StandardOutput.ReadToEnd());
Console.ReadKey();
}
Upvotes: 2
Reputation: 31231
Try this code instead:
string path = @"C:\mypsscript.bat";
Process p = new Process();
p.StartInfo.FileName = path;
p.StartInfo.Arguments = "xyz";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
Console.WriteLine(p.StandardOutput.ReadToEnd());
Console.ReadKey();
For the sakes of debugging I have created a batch file with the code:
echo %1
When I run the above code I get:
xyz
So that seems to work fine. Try using a batch file like this and see if it works, if it does it may be an association with powershell scripts that isn't working, which we can fix later.
Upvotes: 1