简体   繁体   English

使用Process.Start打开pdf文件

[英]Opening pdf files using Process.Start

I am trying to open PDF files in Adobe reader using C#'s Process.Start() . 我正在尝试使用C#的Process.Start()在Adobe Reader中打开PDF文件。

When I provide a path without white spaces it works fine, but paths and pdf files containing white spaces don't open. 当我提供没有空格的路径时,它可以正常工作,但是包含空格的路径和pdf文件无法打开。

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 . 如果它是C:\\\\Users\\\\Manish\\\\Documents\\\\ms_Essential_.NET_4.5.pdf则可以正常运行,但如果它是F:\\\\Tutorials\\\\C#\\\\Foundational\\\\Microsoft Visual C# 2012 Step By Step V413HAV.pdf Adobe Reader出现错误,指出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. 我已经在SO中通读了许多与此主题相关的问题,但是它不起作用。 As I can't figure out how to apply @ prefix in my string s . 由于我不知道如何在字符串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. 只是一个小技巧,客户端上设置了默认的PDF阅读器:如果使用该过程,只需将文件名用作FileName 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 + "\"";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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