c#使用默认应用程序和参数打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11365984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# open file with default application and parameters
提问by álvaro García
The most easy way to open a file with the default application is:
使用默认应用程序打开文件的最简单方法是:
System.Diagnostics.Process.Start(@"c:\myPDF.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.
我知道如何创建一个新进程并设置参数,但是这种方式我需要指示应用程序的路径,并且我想要一个可移植的应用程序,而不必每次都设置应用程序的路径我在其他计算机上使用该应用程序。我的想法是,我希望计算机已经安装了pdf阅读器,并且只说要打开的页面。
Thanks.
谢谢。
采纳答案by daniloquio
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 或 Reader,请继续阅读:
You can do it without telling the full Acrobat path, like this:
您可以在不告知完整 Acrobat 路径的情况下执行此操作,如下所示:
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();
If you don't want the pdf to open with Reader but with Acrobat, chage the second line like this:
如果您不想使用 Reader 打开 pdf,而是使用 Acrobat,请像这样更改第二行:
myProcess.StartInfo.FileName = "Acrobat.exe";
You can query the registry to identify the default application to open pdf files and then define FileName on your process's StartInfo accordingly.
您可以查询注册表以识别打开 pdf 文件的默认应用程序,然后相应地在进程的 StartInfo 上定义 FileName。
Follow this question for details on doing that: Finding the default application for opening a particular file type on Windows
请按照此问题获取有关执行此操作的详细信息:查找用于在 Windows 上打开特定文件类型的默认应用程序
回答by Aghilas Yakoub
you can try with
你可以试试
Process process = new Process();
process.StartInfo.FileName = "yourProgram.exe";
process.StartInfo.Arguments = ..... //your parameters
process.Start();
回答by HatSoft
Please add Settings under Properties for the Project and make use of them this way you have clean and easy configurable settings that can be configured as default
请在项目的属性下添加设置并以这种方式使用它们,您将拥有干净且易于配置的设置,可以将其配置为默认值
How To: Create a New Setting at Design Time
Update: after comments below
更新:在下面的评论之后
- Right + Click on project
- Add New Item
- Under Visual C# Items -> General
- Select Settings File
- 右键 + 单击项目
- 添加新项目
- 在 Visual C# 项下 -> 常规
- 选择设置文件
回答by Ohad Schneider
I converted the VB code in the blog post linked by xslto C# and modified it a bit:
我把xsl链接的博文中的VB代码转换成C#,稍微修改了一下:
public static bool TryGetRegisteredApplication(
string extension, out string registeredApp)
{
string extensionId = GetClassesRootKeyDefaultValue(extension);
if (extensionId == null)
{
registeredApp = null;
return false;
}
string openCommand = GetClassesRootKeyDefaultValue(
Path.Combine(new[] {extensionId, "shell", "open", "command"}));
if (openCommand == null)
{
registeredApp = null;
return false;
}
registeredApp = openCommand
.Replace("%1", string.Empty)
.Replace("\"", string.Empty)
.Trim();
return true;
}
private static string GetClassesRootKeyDefaultValue(string keyPath)
{
using (var key = Registry.ClassesRoot.OpenSubKey(keyPath))
{
if (key == null)
{
return null;
}
var defaultValue = key.GetValue(null);
if (defaultValue == null)
{
return null;
}
return defaultValue.ToString();
}
}
EDIT - this is unreliable.See Finding the default application for opening a particular file type on Windows.
编辑 - 这是不可靠的。请参阅查找用于在 Windows 上打开特定文件类型的默认应用程序。
回答by Jessie Lesbian
this should be close!
这应该接近了!
public static void OpenWithDefaultProgram(string path)
{
Process fileopener = new Process();
fileopener.StartInfo.FileName = "explorer";
fileopener.StartInfo.Arguments = "\"" + path + "\"";
fileopener.Start();
}

