Mono:System.ComponentModel.Win32Exception

例子中console.exe和time.exe都是.NET控制台程序,time.exe在窗口显示当前时间,console.exe调用它并显示返回的内容(即时间)。

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;
            process.StartInfo.FileName = "mono";
            process.StartInfo.Arguments = "time.exe";

            process.Start();
            process.WaitForExit();
            Console.WriteLine(process.StandardOutput.ReadToEnd());
            process.Close();
        }
    }
}

再测试,正常了!

引用内容 引用内容
[root@localhost soft]# mono console.exe
11-6-24 下06时14分35秒


评论: 0 | 引用: 0 | 查看次数: 5191
发表评论
登录后再发表评论!