views:

676

answers:

2

Hello everyone. 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  A: 

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.

shahkalpesh
LinkLabel in WinForms is just another control. It looks like a hyperlink, but it does whatever the programmer wants it to do.
MusiGenesis
Thanks for your answer. I get your point, but it should not result in unhandled exception. I have added my code to the question.
kobra
what is the code inside the form show? Are you making a call to Process.Start?
shahkalpesh
Thanks. I am not making a call to Process.Start. I have added the code in the form to my question.Regards
kobra
Thanks I tried as suggested. The exception is being caught.exception caught: 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 testforso.Program.Main(String[] args) in C:\Users\Gautam\Documents\VisualStudio 2005\Projects\GUID\testforso\testforso\Program.cs:line 18
kobra
Did you search for a call to Process.Start inside your cs files?
shahkalpesh
Thanks. You are right. I had Process.Start("help.html") in the linklabel_click event. But I removed it and added the code as shown in my question. Regards
kobra
I don't have more than one event handler registered to handle click event. Regards
kobra
How are you running the application? If you are running the exe without the IDE, are you running the right exe? i.e. you will need to rebuild the project in release mode before running the exe outside of the IDE. I am pretty sure, you are running the exe that is not latest.
shahkalpesh
shahkalpesh
Thanks for all the help.
kobra
So, what was the actual problem? If you think, my answer helped you nail it down - you can accept it. If not, add your own answer to this explaining how you resolved it.
shahkalpesh
Your suggestions were helpful, but MusiGenesis suggestion to remove Uri was the solution I which worked for me. Still not clear though why try-catch did not work, but solved the problem. Thanks and Regards
kobra
+2  A: 

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

MusiGenesis
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
kobra
See my update.
MusiGenesis
After looking into Uri methods, I don't think my code above will work. What type of object is "wbHelp"?
MusiGenesis
Thanks. wbHelp is a webbrwser control in the form FrmHelp. Regards
kobra
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
@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.
MusiGenesis