【问题标题】:c# open file with default application and parametersc# 使用默认应用程序和参数打开文件
【发布时间】:2012-07-07 03:15:26
【问题描述】:

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

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

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

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

谢谢。

【问题讨论】:

  • 您是指将参数发送到 Adob​​e 可执行文件而不是 pdf 文件,但不使用完整路径?
  • 您希望它如何工作?如果不知道应用程序的路径,就不知道默认的PDF查看器是哪个,也不知道使用哪种参数格式。
  • 在不给出应用程序的完整路径的情况下说myProcess.StartInfo.FileName = "Acrobat.exe"; 还不够吗?
  • 看看这个SO question 看看有没有帮助
  • @daniloquio:我认为关键是 OP 不知道最终用户在他/她的机器上安装了什么来读取 adobe 文件,可能是 Acrobat、Acrobat Reader 或其他东西否则。

标签: c# file


【解决方案1】:

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

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


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

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,请像这样更改第二行:

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

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

请关注此问题以获取有关执行此操作的详细信息:Finding the default application for opening a particular file type on Windows

【讨论】:

  • +1 另外,我认为可以查找与任何文件类型关联的应用程序,例如'.pdf' 在注册表中,然后将该名称放在文件名参数中。见stackoverflow.com/questions/162331/…
  • 是否有一些 open 参数可以让 Adob​​e 阅读器在 2 页视图中打开?只是好奇,如果有人知道,那将非常有用。我一直在四处寻找,但找不到任何东西。
  • 这里有一个解决方法,不需要您指定可执行文件。只需调用一个命令行,它就会为您找出可执行文件。例如Process.Start("cmd /C test.pdf")。虽然可能不适合生产软件,但这是确保 PDF 可以在不同机器上打开的完全可接受的方式;可能适用于内部工具。
【解决方案2】:

这应该很接近了!

public static void OpenWithDefaultProgram(string path)
{
    using Process fileopener = new Process();

    fileopener.StartInfo.FileName = "explorer";
    fileopener.StartInfo.Arguments = "\"" + path + "\"";
    fileopener.Start();
}

【讨论】:

  • 太完美了!
  • 这对我用默认程序打开 PDF 很有用。谢谢!
  • @imgen 问题是关于提供页码作为参数。这个答案是否涵盖了这一点?
  • @daniloquio 抱歉,我没有深入了解这个问题。
  • 别忘了把它包装在 using 块中,Process 是 IDisposable。
【解决方案3】:

我将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();
    }
}

编辑 - 这是不可靠的。Finding the default application for opening a particular file type on Windows

【讨论】:

    【解决方案4】:

    请在项目的属性下添加设置并以这种方式使用它们,您可以获得干净且易于配置的设置,可以配置为默认设置

    How To: Create a New Setting at Design Time

    更新:在下面的cmets之后

    1. 右键+点击项目
    2. 添加新项目
    3. 在 Visual C# 项下 -> 常规
    4. 选择设置文件

    【讨论】:

      【解决方案5】:

      你可以试试

      Process process = new Process();
      process.StartInfo.FileName = "yourProgram.exe";
      process.StartInfo.Arguments = ..... //your parameters
      process.Start();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-20
        • 1970-01-01
        • 1970-01-01
        • 2014-01-31
        • 1970-01-01
        • 2020-08-29
        相关资源
        最近更新 更多