Search code examples
c#exceptionlinklabel

Unhandled exception when Linklabel is clicked C# Winform


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.


Solution

  • 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).