简体   繁体   中英

Opening pdf files using Process.Start

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?

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.

尝试将引号引起来:

info.Arguments = "\"" + s + "\"";

在字符串值之前使用字符@应该起作用:

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

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 + "\"";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM