进程捕获异常
我有一个启动 .exe 并引发异常的进程。有没有办法让进程捕获异常?
到目前为止我已经尝试过了:
Assembly assembly = Assembly.LoadFrom("ConsoleApplication1.exe");
Type[] types = assembly.GetTypes();
foreach (Type t in types)
{
MethodInfo method = t.GetMethod("Main",
BindingFlags.Static | BindingFlags.NonPublic);
if (method != null)
{
try
{
method.Invoke(null, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
break;
}
}
但我在第一行遇到异常,说找不到 .exe。 我也尝试过这个:
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "C:\\Users\\John\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\ConsoleApplication1.EXE";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
但是当我调试时,没有任何异常,并且ConsoleApplication1.exe没有出现。
I have a process which starts an .exe which throws exception. Is there a way for the process to catch the exception?
I've tried this so far:
Assembly assembly = Assembly.LoadFrom("ConsoleApplication1.exe");
Type[] types = assembly.GetTypes();
foreach (Type t in types)
{
MethodInfo method = t.GetMethod("Main",
BindingFlags.Static | BindingFlags.NonPublic);
if (method != null)
{
try
{
method.Invoke(null, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
break;
}
}
but I get an exception on the first line, saying that the .exe can't be found.
I've also tried this:
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "C:\\Users\\John\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\ConsoleApplication1.EXE";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
but when I debug, there isn't any exception, and the ConsoleApplication1.exe doesn't show up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
System.IO.Path.GetFullPath("ConsoleApplication1.exe")
link,应该返回完整路径
Try this:
System.IO.Path.GetFullPath("ConsoleApplication1.exe")
link, which should return the full path
您可以只在代码的第一行使用 try/catch,然后处理 catch。
否则你总是可以尝试这个:
希望这有帮助。
You could just use the try/catch on the first line of your code and then process the catch.
And otherwise you always could try this:
Hope this helps.