c#使用默认应用程序和参数打开文件 [英] c# open file with default application and parameters

查看:79
本文介绍了c#使用默认应用程序和参数打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用默认应用程序打开文件的最简单方法是:

The most easy way to open a file with the default application is:

System.Diagnostics.Process.Start(@"c:myPDF.pdf");

但是,我想知道是否存在为默认应用程序设置参数的方法,因为我想以确定的页码打开 pdf.

However, I would like to know if exists a way to set parameters to the default application, because I would like to open a pdf in a determinate page number.

我知道如何创建一个新进程并设置参数,但是这样我需要指明应用程序的路径,而且我想要一个可移植的应用程序而不是必须设置应用程序的路径每次我在其他计算机上使用该应用程序时.我的想法是,我希望电脑已经安装了pdf阅读器,只说打开什么页面.

I know how to do it creating a new process and setting the parameters, but this way I need to indicate the path of the application, and I would like to have a portable application and not to have to set the path of the application each time I use the application in other computer. My idea is that I expect that the computer has installed the pdf reader and only say what to page open.

谢谢.

推荐答案

如果你想用默认应用程序打开文件,我的意思是不指定 Acrobat 或 Reader,你不能在指定页面打开文件.

If you want the file to be opened with the default application, I mean without specifying Acrobat or Reader, you can't open the file in the specified page.

另一方面,如果您可以指定 Acrobat 或 Reader,请继续阅读:

On the other hand, if you are Ok with specifying Acrobat or Reader, keep reading:

您可以在不告知完整的 Acrobat 路径的情况下执行此操作,如下所示:

You can do it without telling the full Acrobat path, like this:

using Process myProcess = new Process();    
myProcess.StartInfo.FileName = "acroRd32.exe"; //not the full application path
myProcess.StartInfo.Arguments = "/A "page=2=OpenActions" C:\example.pdf";
myProcess.Start();

如果您不想使用 Reader 而使用 Acrobat 打开 pdf,请像这样更改第二行:

If you don't want the pdf to open with Reader but with Acrobat, chage the second line like this:

myProcess.StartInfo.FileName = "Acrobat.exe";

您可以查询注册表以识别打开 pdf 文件的默认应用程序,然后相应地在进程的 StartInfo 上定义 FileName.

You can query the registry to identify the default application to open pdf files and then define FileName on your process's StartInfo accordingly.

按照此问题了解有关执行此操作的详细信息:查找用于在 Windows 上打开特定文件类型的默认应用程序

Follow this question for details on doing that: Finding the default application for opening a particular file type on Windows

这篇关于c#使用默认应用程序和参数打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆