单击 Linklabel 时出现未处理的异常 C# Winform

发布于 2024-08-04 17:51:24 字数 2291 浏览 5 评论 0原文

当我单击应该打开表单的链接标签时,出现奇怪的未处理异常。我尝试将代码放入 try-catch 块中的 linklabel_click 事件处理程序中,但仍然收到以下错误。

请参阅此消息的末尾 有关即时调用的详细信息 (JIT) 调试而不是此对话框 盒子。
************** 异常文本 ************** System.ComponentModel.Win32Exception: 系统找不到该文件 指定于 System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo 开始信息)位于 System.Diagnostics.Process.Start()
在 系统.诊断.进程.启动(ProcessStartInfo 开始信息)位于 System.Diagnostics.Process.Start(字符串 文件名)位于 InfoCapsule.FrmLink.llblHelp_LinkClicked(对象 发件人、LinkLabelLinkClickedEventArgs 吃 System.Windows.Forms.LinkLabel.OnLinkClicked(LinkLabelLinkClickedEventArgs 吃 System.Windows.Forms.LinkLabel.OnMouseUp(MouseEventArgs 吃 System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons 按钮,Int32 单击) 在 System.Windows.Forms.Control.WndProc(Message& 米)在 System.Windows.Forms.Label.WndProc(Message& 米)在 System.Windows.Forms.LinkLabel.WndProc(Message& 味精)在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& 米)在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& 米)在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd、Int32 消息、IntPtr wparam、IntPtr l参数)

linklabel_click 的代码如下。

private void llblHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    try
    {
        refFrmHelp = new FrmHelp(this);
        refFrmHelp.Show();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

FrmHelp 中的代码

            String sitePath = null;
            try
            {
                sitePath = "file:///" + Application.StartupPath + "\\help.html";
                //sitePath = sitePath.Replace("\\", "/");
                MessageBox.Show(sitePath);
                Uri path = new Uri(sitePath);
                wbHelp.Navigate(path);
            }
            catch (UriFormatException ex)
            {
                MessageBox.Show(ex.ToString() + "\nSite Path: " + sitePath);
                return false;
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString() + "\nSite Path: " + sitePath);
                return false;
            }

您能帮我调试吗?

I am getting a strange unhandled exception when I click the linklabel which should open a form. I have tried to put the code in linklabel_click event handler in try-catch block, but I still get the error below.

See the end of this message for
details on invoking just-in-time
(JIT) debugging instead of this dialog
box.
************** Exception Text ************** System.ComponentModel.Win32Exception:
The system cannot find the file
specified at
System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo
startInfo) at
System.Diagnostics.Process.Start()
at
System.Diagnostics.Process.Start(ProcessStartInfo
startInfo) at
System.Diagnostics.Process.Start(String
fileName) at
InfoCapsule.FrmLink.llblHelp_LinkClicked(Object
sender, LinkLabelLinkClickedEventArgs
e) at
System.Windows.Forms.LinkLabel.OnLinkClicked(LinkLabelLinkClickedEventArgs
e) at
System.Windows.Forms.LinkLabel.OnMouseUp(MouseEventArgs
e) at
System.Windows.Forms.Control.WmMouseUp(Message&
m, MouseButtons button, Int32 clicks)
at
System.Windows.Forms.Control.WndProc(Message&
m) at
System.Windows.Forms.Label.WndProc(Message&
m) at
System.Windows.Forms.LinkLabel.WndProc(Message&
msg) at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&
m) at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m) at
System.Windows.Forms.NativeWindow.Callback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr
lparam)

The code for linklabel_click is as under.

private void llblHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    try
    {
        refFrmHelp = new FrmHelp(this);
        refFrmHelp.Show();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Code inside FrmHelp

            String sitePath = null;
            try
            {
                sitePath = "file:///" + Application.StartupPath + "\\help.html";
                //sitePath = sitePath.Replace("\\", "/");
                MessageBox.Show(sitePath);
                Uri path = new Uri(sitePath);
                wbHelp.Navigate(path);
            }
            catch (UriFormatException ex)
            {
                MessageBox.Show(ex.ToString() + "\nSite Path: " + sitePath);
                return false;
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString() + "\nSite Path: " + sitePath);
                return false;
            }

Can you please help me in debugging.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

谈场末日恋爱 2024-08-11 17:51:24

我刚刚使用 WebBrowser 控件对此进行了测试,您可以导航到本地文件,而根本不用担心 Uri 类。这段代码应该适合你:

string sitePath = Application.StartupPath + @"\help.html";
wbHelp.Navigate(sitePath);

Uri 有时有点奇怪,尽管我以前从未见过它们抛出无法捕获的异常(尽管可能是 WebBrowser 抛出异常 - 我不知道)。

确保运行此代码时“help.html”实际上位于应用程序的启动文件夹中,否则 Web 浏览器将显示“此页面无法显示...”消息。如果您从 Visual Studio 运行应用程序,则 Application.StartupPath 将位于项目文件夹中的“\bin\Debug\”或“\bin\Release\”子文件夹中(具体取决于您是否在调试或发布模式下运行)。

I just tested this with a WebBrowser control, and you can navigate to a local file without bothering with the Uri class at all. This code should work for you:

string sitePath = Application.StartupPath + @"\help.html";
wbHelp.Navigate(sitePath);

Uri's are kind of quirky sometimes, although I've never seen them throw an uncatchable exception before (although it might be the WebBrowser throwing the exception - I dunno).

Make sure when you run this code that "help.html" is actually in the application's startup folder, or the WebBrowser will display a "this page cannot be displayed ..." message. If you're running your application from Visual Studio, the Application.StartupPath will be in your project's folder, in the "\bin\Debug\" or the "\bin\Release\" sub-folder (depending on whether you're running it in Debug or Release mode).

绝對不後悔。 2024-08-11 17:51:24

查看异常,您似乎正在提供指向本地/网络位置的链接 - 这不是有效的路径。

编辑:Linklabel 的作用类似于超链接。它不应该用于在应用程序内打开表单


EDIT2:链接的目标是什么?尝试将其设置为适当的 URL &看看会发生什么。我想,如果它是正确的 URL,它应该随 URL 一起打开表单。

EDIT3:将其放入控制台应用程序的主要方法中看看会发生什么。

    try
    {
        Process.Start("c:\\calc.exe");
    }
    catch (Exception e)
    {
        Console.WriteLine("exception caught: " + e);
    }

我认为,您应该正确放置路径以确保不会发生异常。
正如我之前所说,链接的目标是什么?

EDIT4:我很抱歉造成混乱。 MusiGenesis 是对的。它是一个普通链接,无法自行执行。在代码中查找 Process.Start 方法调用。

我会建议重建该项目。 在调用 Process.Start 之前您是否有代码?

顺便说一句,看看您是否有超过 1 个事件处理程序注册来处理点击。

Looking at the exception, it seems you are providing a link to the local/network location - which is not a valid path.

EDIT: Linklabel is meant to act like a hyperlink. It should not be used to open a form inside the application


EDIT2: What is the target for the link? Try setting it to an appropriate URL & see what happens. If it is a proper URL, it should open the form alongwith the URL, I guess.

EDIT3: Put this inside the main method of a console app & see what happens.

    try
    {
        Process.Start("c:\\calc.exe");
    }
    catch (Exception e)
    {
        Console.WriteLine("exception caught: " + e);
    }

I think, you should put the path correctly to make sure that the exception doesn't occur.
As I said before, what is the link's target?

EDIT4: I am sorry for the confusion. MusiGenesis is right. It is a plain link, which cannot execute on its own. Find inside your code for Process.Start method call.

I will suggest re-building the project. Did you have/had code before that made a call to Process.Start?

On a side note, see if you have more than 1 event handlers registered to handle the click.

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