进程捕获异常

发布于 2024-12-10 08:11:49 字数 966 浏览 1 评论 0原文

我有一个启动 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟燃烟灭 2024-12-17 08:11:49

试试这个:

System.IO.Path.GetFullPath("ConsoleApplication1.exe")

link,应该返回完整路径

Try this:

System.IO.Path.GetFullPath("ConsoleApplication1.exe")

link, which should return the full path

北陌 2024-12-17 08:11:49

您可以只在代码的第一行使用 try/catch,然后处理 catch。

try { Assembly assembly = Assembly.LoadFrom("ConsoleApplication1.exe"); }
catch { throw; }

否则你总是可以尝试这个:

myProcess.StartInfo.FileName = @"C:\Where\Your\File\Is.exe";

希望这有帮助。

You could just use the try/catch on the first line of your code and then process the catch.

try { Assembly assembly = Assembly.LoadFrom("ConsoleApplication1.exe"); }
catch { throw; }

And otherwise you always could try this:

myProcess.StartInfo.FileName = @"C:\Where\Your\File\Is.exe";

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文