我如何从文本框中转义文本?

时间:2016-03-17 14:01:37

标签: c# winforms

我有一个应用程序,它从sql查询中填充文本框。

在我的sql表上,我的服务器位置显示为\\disk\path\path2\file.pdf

在我的应用文本框中,它也会显示为\\disk\path\path2\file.pdf

但是在我的代码中,我有一个按钮,用Process.Start(scanDLTextBox.Text);

打开文件

但是在我调试时,我的文本框显示为scanDLTextBox.Text "\\\\disk\\path\\path2\\file.pdf" string.

因为额外\我收到了错误

  

系统找不到指定的文件

所以我的问题是,如何从文本框中删除额外的\

Process.Start(scanDLTextBox.Text.Replace(@"\\", @"\"));不会删除额外的\

4 个答案:

答案 0 :(得分:1)

调试器会向您显示额外的\,但是如果您点击旁边的放大器,您将看到正确的字符串值。

尝试查看文件是否确实存在:

        string path = scanDLTextBox.Text;

        FileInfo fi = new FileInfo(path);

        bool exists = fi.Exists;

此外,如果是网络驱动器,您是否可以访问它?

如果您使用Process启动它,请尝试使用:

Process process = new Process();
process.StartInfo.FileName = @"\\disk\path\path2\file.pdf";
process.StartInfo.UseShellExecute = true;
process.StartInfo.ErrorDialog = true; 
process.Start();

检查这个ErrorDialog属性,它应该要求验证。

答案 1 :(得分:0)

尝试以下

var path = Regex.Replace(scanDLTextBox.Text, @"[\\]{2,}", @"\");

答案 2 :(得分:0)

我建议一个简单的循环:

   private static String UnEscape(String source) {
     if (String.IsNullOrEmpty(source)) 
       return source;

     StringBuilder Sb = new StringBuilder(source.Length);

     for (int i = 0; i < source.Length; ++i) {
       Char ch = source[i];

       Sb.Append(ch);

       if ((ch == '\\') && (i < source.Length - 1) && (source[i + 1] == '\\'))
         i += 1; // skip next slash \
     }

     return Sb.ToString();
   }

   ...

   String source = @"\\\\disk\\path\\path2\\file.pdf";

   // "\\disk\path\path2\file.pdf"
   String result = UnEscape(source);

答案 3 :(得分:-1)

将文字分配给变量:

string text1 = scanDLTextBox.Text;

的Process.Start(文本);

如果文本确实被转义,那么在使用IntelliSense时无法检查。例如,你需要.Replace()反斜杠。

相关问题