2

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.

1
  • It should be mentioned that you have both forward and back slashes in your file path. Commented Sep 12, 2009 at 5:08

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. But I don't understand why it is an unhandled exception. If there is an exception in FrmHelp it should be caught in the try-catch block of the calling method. Regards
After looking into Uri methods, I don't think my code above will work. What type of object is "wbHelp"?
Thanks. wbHelp is a webbrwser control in the form FrmHelp. Regards
Thanks for all the help. I am using a local html file, so I will not use Uri. But still amazed why try-catch is not working. Regards
@kobra: as an experiment, I just tried using a Uri like in your code, and it worked fine on my machine, so I don't think the Uri was the problem. The WebBrowser control is just a host for an instance of Internet Explorer, so I suspect that the uncatchable error is being thrown by IE.
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.

12 Comments

LinkLabel in WinForms is just another control. It looks like a hyperlink, but it does whatever the programmer wants it to do.
Thanks for your answer. I get your point, but it should not result in unhandled exception. I have added my code to the question.
what is the code inside the form show? Are you making a call to Process.Start?
Thanks. I am not making a call to Process.Start. I have added the code in the form to my question. Regards
Thanks I tried as suggested. The exception is being caught. exception caught: System.ComponentModel.Win32Exception: The system cannot find t he file specified at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startI nfo) at System.Diagnostics.Process.Start() at System.Diagnostics.Process.Start(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start(String fileName) at testforso.Program.Main(String[] args) in C:\Users\Gautam\Documents\Visual Studio 2005\Projects\GUID\testforso\testforso\Program.cs:line 18
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.