time.exe:
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now);
}
}
}console.exe:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = "time.exe";
process.Start();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
process.Close();
}
}
}在Windows7中执行正常的,但在RHEL5下用Mono运行却出错了,提示:
[root@localhost soft]# mono console.exe
Unhandled Exception: System.ComponentModel.Win32Exception: ApplicationName='time.exe', CommandLine='', CurrentDirectory=''
at System.Diagnostics.Process.Start_noshell (System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process process) [0x00000] in <filename unknown>:0
at System.Diagnostics.Process.Start_common (System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process process) [0x00000] in <filename unknown>:0
at System.Diagnostics.Process.Start () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Diagnostics.Process:Start ()
at ConsoleApplication1.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
错误的原因是在RHEL5下time.exe也应通过Mono来运行,修改console.exe代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.RedirectStandardOutput = true;
[color=Red]process.StartInfo.FileName = "mono";
process.StartInfo.Arguments = "time.exe";[/color]
process.Start();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
process.Close();
}
}
}再测试,正常了!
[root@localhost soft]# mono console.exe
11-6-24 下06时14分35秒
评论(0)
暂无评论。