You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

按Stack Overflow方案实现RichTextBox链接点击时提示找不到文件,原因何在?

RichTextBox超链接点击报错原因解析

问题重现

按以下操作后触发错误:

  1. 为RichTextBox控件的LinkClicked事件绑定处理方法,代码如下:
private void mRichTextBox_LinkClicked (object sender, LinkClickedEventArgs e) {
    System.Diagnostics.Process.Start(e.LinkText);
}
  1. 点击RichTextBox内的超链接时,抛出错误:
System.ComponentModel.Win32Exception: 'An error occurred trying to start process 'https://example.com' with working directory 'XYZ'. The system cannot find the file specified.'

报错原因

直接调用Process.Start(string)方法时,系统会把传入的URL当作可执行文件路径处理,而非识别为需要用浏览器打开的网络链接。由于系统找不到名为https://example.com的可执行程序,因此抛出"找不到文件"的错误。

解决方法

需要明确告知系统用默认浏览器打开该URL,以下是两种常用实现方式:

方式1:使用ProcessStartInfo指定启动规则

private void mRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
{
    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo
    {
        FileName = e.LinkText,
        UseShellExecute = true
    };
    System.Diagnostics.Process.Start(psi);
}

UseShellExecute = true会让系统调用与URL关联的默认程序(如浏览器)来处理链接,而非将其当作可执行文件启动。

方式2:直接传入Uri对象

private void mRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
{
    System.Diagnostics.Process.Start(new System.Uri(e.LinkText));
}

这种方式下,Process.Start会自动识别Uri类型,调用默认浏览器打开目标链接。


内容的提问来源于stack exchange,提问作者Ooker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 09:20:23