当我单击应打开表单的链接标签时,出现奇怪的未处理异常。我尝试将代码放入 try-catch block 中的 linklabel_click 事件处理程序中,但仍然收到以下错误。
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)
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;
}
你能帮我调试一下吗?
最佳答案
我刚刚使用 WebBrowser 控件对此进行了测试,您可以导航到本地文件,而根本不用担心 Uri 类。此代码应该适合您:
string sitePath = Application.StartupPath + @"\help.html";
wbHelp.Navigate(sitePath);
Uri 有时有点古怪,尽管我以前从未见过它们抛出无法捕获的异常(尽管可能是 WebBrowser 抛出异常 - 我不知道)。
确保运行此代码时“help.html”实际上位于应用程序的启动文件夹中,否则 Web 浏览器将显示“此页面无法显示...”消息。如果您从 Visual Studio 运行应用程序,则 Application.StartupPath 将位于项目文件夹中的“\bin\Debug\”或“\bin\Release\”子文件夹中(具体取决于您是否在调试或 Release模式下运行)。
关于c# - 单击 Linklabel 时出现未处理的异常 C# Winform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1414011/