C# Process.StartInfo.UseShellExecute相关错误
我有一个c#控制台应用程序,它使用Process.Start()方法执行任何脚本文件。我提供了进程1.StartInfo.FileName的脚本文件路径。我的脚本文件可以是任何类型的(.vbs、ps1等)。我还使用指令C# Process.StartInfo.UseShellExecute相关错误,c#,.net,C#,.net,我有一个c#控制台应用程序,它使用Process.Start()方法执行任何脚本文件。我提供了进程1.StartInfo.FileName的脚本文件路径。我的脚本文件可以是任何类型的(.vbs、ps1等)。我还使用指令p.StartInfo.Arguments将字符串传递给脚本。当脚本文件执行时,它应该将字符串重新运行回c#应用程序。可以通过设置Process1.StartInfo.RedirectStandardOutput=true来读取返回的字符串,但要使用此指令,我需要设置Process
p.StartInfo.Arguments
将字符串传递给脚本。当脚本文件执行时,它应该将字符串重新运行回c#应用程序。可以通过设置Process1.StartInfo.RedirectStandardOutput=true
来读取返回的字符串,但要使用此指令,我需要设置Process1.StartInfo.UseShellExecute=false
。
当我运行这个程序时,我得到的错误是“指定的可执行文件不是有效的Win32应用程序”。
我想这可能是因为,当我设置Process1.StartInfo.UseShellExecute=false
时,我的应用程序不知道使用哪个.exe来执行脚本文件
另一方面,如果我提供指向StartInfo.FileName
的exe路径和指向StartInfo.Argument
的脚本文件路径,那么我不会得到错误。
例如:我想执行powershell脚本,并将以下属性设置为P1.StartInfo.FileName=“powershell.exe的位置”
和P1.StartInfo.Argument=“.ps1脚本文件路径”
,在这种情况下,我不会得到错误
问题是我事先不知道要执行哪种类型的脚本。也找不到在不同m/c上执行脚本文件的.exe文件的位置。那么,是否可以从同一个公共c#appln执行不同类型的脚本文件,并读取脚本返回的输出
这是我的密码
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());
}
}
}
请尝试以下代码:
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();
为了调试,我创建了一个批处理文件,代码如下:
echo %1
当我运行上述代码时,我得到:
xyz
所以这似乎很有效。尝试使用这样的批处理文件,看看它是否有效,如果有效,可能是与powershell脚本的关联不起作用,我们可以稍后修复。请尝试以下代码:
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();
为了调试,我创建了一个批处理文件,代码如下:
echo %1
当我运行上述代码时,我得到:
xyz
所以这似乎很有效。尝试使用这样的批处理文件,看看它是否有效,如果有效,可能是与powershell脚本的关联不起作用,我们可以稍后修复。您可以检查脚本类型并从自己的引擎读取输出
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();
}
您可以检查脚本类型并从自己的引擎读取输出
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();
}
@Bail C:当我使用P1.StartInfo.FileName=@“C:\mypsscript.ps1”时出错。错误:指定的可执行文件不是有效的Win32应用程序。由于“p.StartInfo.UseShellExecute=false;”指令,无论是否要重定向输出,我都会出现此错误。@sailer在您发布代码后,我已重写了我的答案,请尝试此方法进行一些调试。@Bail C:ur代码和我的代码对于批处理文件也可以正常工作。但不适用于.ps1和.vbs文件。My.ps1文件只包含一行“写入输出”Hello world“@sailer嗯,这很奇怪,我不知道为什么这样不行,抱歉。等待,看看是否有其他人对此有答案。如果没有其他原因,至少我们知道批处理文件可以工作:)但不管出于什么原因,powershell和vb不能。@C:我在使用P1.StartInfo.FileName=@“C:\mypsscript.ps1”时出错。错误:指定的可执行文件不是有效的Win32应用程序。由于“p.StartInfo.UseShellExecute=false;”指令,无论是否要重定向输出,我都会出现此错误。@sailer在您发布代码后,我已重写了我的答案,请尝试此方法进行一些调试。@Bail C:ur代码和我的代码对于批处理文件也可以正常工作。但不适用于.ps1和.vbs文件。My.ps1文件只包含一行“写入输出”Hello world“@sailer嗯,这很奇怪,我不知道为什么这样不行,抱歉。等待,看看是否有其他人对此有答案。如果没有其他东西,至少我们知道批处理文件可以工作:)只是powershell和vb不能工作,不管出于什么原因。:我想将字符串传递给脚本,我怎么做?只需将字符串附加到
process.StartInfo.Arguments
,编辑代码。:我想将字符串传递给脚本,我怎么做?只需将字符串附加到process.StartInfo.Arguments
,编辑代码。