我试图执行一个进程,就像它在窗口的命令行上执行一样,但是进程类不允许可执行文件创建一个类似于从命令行运行它会执行的文件。进程将在部署时以定时间隔在服务器上异步运行。命令行将调用如下所示:
curl.exe url -o 数据
wgrib2.exe 数据 -csv output.csv
在下面的代码中,我找到了 curl.exe 的解决方法,但是当它被读取为 StandardOutput 时,该文件会切断该文件类型的一些关键停止字符。
public static void httpQuery(string queryString)
{
Process myProcess = new Process();
try
{
string basepath = AppDomain.CurrentDomain.BaseDirectory;
myProcess.StartInfo.UseShellExecute = false; // allows us to redirect the output
myProcess.StartInfo.CreateNoWindow = true; // stop's command window from being made
myProcess.StartInfo.Verb = "runas";
myProcess.StartInfo.FileName = @"C:\Users\Ian's\Desktop\wgrib2\curl.exe";
myProcess.StartInfo.Arguments = queryString; // queryString is http://nomads.noaa.gov/....
myProcess.StartInfo.RedirectStandardError = true; // redirect error to stream reader
myProcess.StartInfo.RedirectStandardOutput = true; // redirect output to strem reader
myProcess.Start();
if (myProcess.WaitForExit(10000) == false)
{
myProcess.Kill();
}
using(StreamReader reader = myProcess.StandardError) // capture error output if any
{
string result = reader.ReadToEnd();
reader.Close();
StreamReader message = myProcess.StandardOutput;
string output = message.ReadToEnd();
StreamWriter writer = new StreamWriter(basepath + "wgrib2\\data");
writer.Write(output);
writer.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
myProcess.Close();
}
此外,我似乎找不到允许第二个进程写入新文件的方法,或者如果它一直是我找不到它正在写入的目录。我在 IIS 开发服务器上使用 VS2012。这是第二个进程代码。
public static void callWgrib2()
{
Process wgrib2 = new Process();
try
{
// call on command line: wgrib2.exe data -csv data.csv
string basepath = AppDomain.CurrentDomain.BaseDirectory; // path to project directory
string arg1 = basepath + "wgrib2\\data"; // filename of operate on
string arg2 =" -csv " + basepath + "wgrib2\\data.csv"; // filename to write to
wgrib2.StartInfo.UseShellExecute = false; // parameters
wgrib2.StartInfo.CreateNoWindow = true;
wgrib2.StartInfo.Verb = "runas";
wgrib2.StartInfo.FileName = @"C:\Users\Ian's\Desktop\wgrib2\wgrib2.exe";
wgrib2.StartInfo.Arguments = "\""+ arg1 + arg2 + "\"";
wgrib2.StartInfo.RedirectStandardError = true;
wgrib2.Start();
if (wgrib2.WaitForExit(10000) == false)
{
wgrib2.Kill();
}
using (StreamReader reader = wgrib2.StandardOutput)
{
string result = reader.ReadToEnd();
reader.Close();
StreamReader err = wgrib2.StandardError;
err.ReadToEnd();
err.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
wgrib2.Close();
}
有人可以指出我的写作方向吗?我已经为这个问题付出了相当大的努力,但我确信有一个我不知道的简单解决方法。