链接到文件的路径与RichTextBox的空间?(Link to File's path w

2019-07-20 19:15发布

我有VS2010,C#。 我用的RichTextBox的形式。 我的DectectUrls属性设置为True。 我设置了LinkClicked事件。

我想开一个这样的文件链接: 文件:// C:\ Documents和Settings ...file:// C:\ Program Files文件(x86)的...

它并不适用于带有空格的路径。

源代码:

rtbLog.SelectionFont = fnormal;
rtbLog.AppendText("\t. Open Path" + "file://" + PathAbsScript + "\n\n");


// 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) {}
}

有什么建议?

Answer 1:

除了使用20%(其中一些用户可能会发现“丑”看),就可以使用Unicode非打破空间字符(U + 00A0)。 例如:

String fileName = "File name with spaces.txt";
FileInfo fi = new FileInfo(fileName);

// Replace any ' ' characters with unicode non-breaking space characters:
richTextBox.AppendText("file://" + fi.FullName.Replace(' ', (char)160));

那么你的内部链接点击处理程序丰富的文本框,你会做到以下几点:

private void richTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
{
    // Replace any unicode non-break space characters with ' ' characters:
    string linkText = e.LinkText.Replace((char)160, ' ');

    // For some reason rich text boxes strip off the 
    // trailing ')' character for URL's which end in a 
    // ')' character, so if we had a '(' opening bracket
    // but no ')' closing bracket, we'll assume there was
    // meant to be one at the end and add it back on. This 
    // problem is commonly encountered with wikipedia links!

    if((linkText.IndexOf('(') > -1) && (linkText.IndexOf(')') == -1))
        linkText += ")";

    System.Diagnostics.Process.Start(linkText);
}


Answer 2:

你应该用双引号,例如路径:

"file://c:\path with spaces\..."

以一个双引号添加到字符串,则必须使用转义序列\"



Answer 3:

去那个特定的文件夹,并给写或使之从该文件夹的属性共享的权限。



Answer 4:

最后,我用一个取代(””, “%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 + "\n\n");

对于LinkClicked事件的代码:

private void rtbLog_LinkClicked(object sender, LinkClickedEventArgs e)
{
            try
            {
                var link = e.LinkText.Replace("%20", " ");
                System.Diagnostics.Process.Start(link);
            }
            catch (Exception)
            {
            }
}


文章来源: Link to File's path with spaces in RichTextBox?