木子屋 Dnawo's BLOG

Mono:System.ComponentModel.Win32Exception

👤 dnawo 📅 2011-06-24 👁 6005 👍 0 💬 0 🔄 来源:本站原创
例子中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;
            [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)

暂无评论。

验证码
评论需审核通过后显示
← 上一篇 RHEL5下用mono运行.NET控制台程序 下一篇 → Ubuntu各版本开发代号一览(更新至14.04)