欢迎光临

木子屋

标题: Mono：System.ComponentModel.Win32Exception

作者: dnawo

日期: 2011-06-24 05:46 PM

分类: Win编程

内容: 例子中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)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.977 ms

返回

标题: Mono：System.ComponentModel.Win32Exception

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.977 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.008 ms

返回