在 RichTextBox 中使用空格链接到文件的路径? [英] Link to File's path with spaces in RichTextBox?
本文介绍了在 RichTextBox 中使用空格链接到文件的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 VS2010,C#.我在表单中使用 RichTextBox.我将 DectectUrls 属性设置为 True.我设置了一个 LinkClicked 事件.
I have VS2010, C#. I use RichTextBox in a form. I set the DectectUrls property to True. I set a LinkClicked event.
我想打开这样的文件链接:file://C:Documents and Settings... 或 file://C:Program Files (x86)...
I would like open a file link like this: file://C:Documents and Settings... or file://C:Program Files (x86)...
它不适用于带空格的路径.
It doesn't works for path with spaces.
源代码:
rtbLog.SelectionFont = fnormal;
rtbLog.AppendText(" . Open Path" + "file://" + PathAbsScript + "
");
// DetectUrls set to true
// launch any http:// or mailto: links clicked in the body of the rich text box
private void rtbLog_LinkClicked(object sender, LinkClickedEventArgs e)
{
try
{
System.Diagnostics.Process.Start(e.LinkText);
}
catch (Exception) {}
}
有什么建议吗?
推荐答案
最后,我使用了替换 (" ", "%20")
Finally, I use a replace (" ", "%20")
// http://social.msdn.microsoft.com/Forums/eu/Vsexpressvb/thread/addc7b0e-e1fd-43f4-b19c-65a5d88f739c
var rutaScript = DatosDeEjecucion.PathAbsScript;
if (rutaScript.Contains(" ")) rutaScript = "file://" + Path.GetDirectoryName(DatosDeEjecucion.PathAbsScript).Replace(" ", "%20");
rtbLog.AppendText(". Abrir ubicación: " + rutaScript + "
");
LinkClicked 事件代码:
The code for LinkClicked event:
private void rtbLog_LinkClicked(object sender, LinkClickedEventArgs e)
{
try
{
var link = e.LinkText.Replace("%20", " ");
System.Diagnostics.Process.Start(link);
}
catch (Exception)
{
}
}
这篇关于在 RichTextBox 中使用空格链接到文件的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文