0

i have an app that populates a textbox from a sql query.

on my sql table, my server location shows as \\disk\path\path2\file.pdf

in my apps textbox it also shows as \\disk\path\path2\file.pdf

but in my code, i have a button that opens the file with Process.Start(scanDLTextBox.Text);

but when im debugging, my textbox shows as scanDLTextBox.Text "\\\\disk\\path\\path2\\file.pdf" string.

because of the extra \ i get the error

The system cannot find the file specified

so my question is, how do i remove the extra \ from the textbox?

Process.Start(scanDLTextBox.Text.Replace(@"\\", @"\")); doesnt remove the extra \

  • How do you check that your `scanDLTextBox.Text` shows `"\\\\disk\\path\\path2\\file.pdf"`? Is it from debugger? If it is, relax! :) It is just the debugger's representation... Your actual string is not changed. If you receive error because you cannot find the file specified, the problem must lie somewhere else. – Ian Mar 17 '16 at 14:03
  • yea its shows in the debugger, pasting the path in run (ctrl + r) takes me straight to the file, so i know the file exists. i just cant access it from my app. any sugguestions as to what can be causing my error? –  Mar 17 '16 at 14:05
  • What is the actual path of the `\\disk`? – Ian Mar 17 '16 at 14:06
  • its a network drive `\\wb-str\index\WKTN\CARS\DRIVERS\213\nia.pdf` –  Mar 17 '16 at 14:07
  • I see.. for network drive, consider using `Utilities.Network.NetworkDrive` utilities. – Ian Mar 17 '16 at 14:14
  • ill look into `Utilities.Network.NetworkDrive` thanks –  Mar 17 '16 at 14:15

4 Answers4

1

Debugger will show you this extra \, but if you click magnificer next to it, you will see the proper string value.

Try to see if file really exists:

        string path = scanDLTextBox.Text;

        FileInfo fi = new FileInfo(path);

        bool exists = fi.Exists;

Also, if its a network drive, are you able to access it?

If you ara using Process to start it, try with:

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

check this ErrorDialog property, it should ask for authentification.

makro88
  • 102
  • 8
  • file exists and im able to access it. pasting the path in run (ctrl + r) takes me straight to the file. i cant think of any other reason as to why it cant access the file. im tinkering with ians answer, i might just hardcode the base path and append the acutal path on runtime. if all else fails –  Mar 17 '16 at 14:32
  • `FileInfo` has `GetAccessControl` - you can check. Also `FileInfo.FullName` could be helpfull. Try to see how it's gonna work with network file. – makro88 Mar 17 '16 at 14:40
0

Try following

var path = Regex.Replace(scanDLTextBox.Text, @"[\\]{2,}", @"\");
tchelidze
  • 8,050
  • 1
  • 29
  • 49
0

I suggest a simple loop:

   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);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
-1

Assign your text to a variable:

string text1 = scanDLTextBox.Text;

Process.Start(text1);

If that doesn't work check with IntelliSense if the text really gets escaped. You need to .Replace() the backslashes then, for example.

Thomas
  • 213
  • 2
  • 9