2

I am trying to open PDF files in Adobe reader using C#'s Process.Start().

When I provide a path without white spaces it works fine, but paths and pdf files containing white spaces don't open.

This is my code:

Button btn = (Button)sender;
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "AcroRd32";
string s = btn.Tag.ToString();
//btn.Tag Contains the full file path 
info.Arguments = s;
Process.Start(info); 

If it is C:\\Users\\Manish\\Documents\\ms_Essential_.NET_4.5.pdf it works fine but if it is F:\\Tutorials\\C#\\Foundational\\Microsoft Visual C# 2012 Step By Step V413HAV.pdf Adobe Reader gives an error saying there was an error in opening the document file can't be found.

I have read through many questions related to this topic in SO but it won't work. As I can't figure out how to apply @ prefix in my string s.

Any ideas how to fix this?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Manish Singh
  • 360
  • 1
  • 6
  • 18

4 Answers4

9

Just a little trick there is a default PDF reader set on the client: just use the file name as FileName if the process. Usually you don't care which program to use, so then this solution just works:

Process.Start(pdfFileName);

This doesn't need special quoting too, so it instantly fixes your problem.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 2
    I've already tried that before posting the question, but I needed it to be program specific, for future references, but, anyway, thanks for your help – Manish Singh Jul 06 '15 at 21:44
7

Try to wrap the arguments around quotes:

info.Arguments = "\"" + s + "\"";
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
1

Using the character @ before the string value should work:

var path = @"F:\Tutorials\C#\Foundational\Microsoft Visual C# 2012 Step By Step V413HAV.pdf";
0

You should enquote the path provided in the argument list. This will cause it to view the path as a single argument instead of multiple space separated arguments:

info.Arguments = "\"" + s + "\"";
Bas
  • 26,772
  • 8
  • 53
  • 86