繁体   English   中英

在Windows机器上有没有办法以编程方式找出哪个应用程序负责打开具有特定扩展名的文件?

[英]On a Windows machine is there a way to programmatically find out which application is responsible for opening files with a particular extension?

在Windows机器上,有没有办法以编程方式找出哪个应用程序负责打开具有特定扩展名的文件? 假设我想以编程方式找出哪个应用程序负责打开.PDF文件。 (如果您有C#或VB.NET代码,请不要关心)

命令行命令ASSOC查找文件关联,命令FTYPE查找分配给它们的操作:

C:\\> assoc .docx
.DOCX = Word.Document.12

C:\\> ftype Word.Document.12
Word.Document.12 =“C:\\ Program Files(x86)\\ Microsoft Office \\ Office12 \\ WINWORD.EXE”/ n / dde

您可以从任何脚本以编程方式调用它们。

从C#开始,您需要执行以下操作:

private string ShellCommand(string command)  
{
  var psi = new ProcessStartInfo("cmd", "/c " + command) {
    RedirectStandardOutput = true,
    CreateNoWindow = true
  };
  var p = Process.Start(psi);
  return p.StandardOutput.ReadToEnd();
}

private string FindDefaultProgram(string extension)
{
   assoc = ShellCommand("assoc " + extension).Split('=')[1];
   program = ShellCommand("ftype " + assoc).Split('=')[1];
   return program;
}

没有测试任何这些,所以拿一粒盐,但这应该让你走在正确的轨道上。

那么,您将从以下位置查看注册表开始:

HKEY_CURRENT_USER \\ SOFTWARE \\微软\\的Windows \\ CurrentVersion \\ Explorer中\\ FileExts.pdf \\ OpenWithList

a和向前将有一个或多个键,指向用于打开该类型文件的程序:

using Microsoft.Win32;
var key = Registry.CurrentUser
    .OpenSubKey("Software")
    .OpenSubKey("Microsoft")
    .OpenSubKey("Windows")
    .OpenSubKey("CurrentVersion")
    .OpenSubKey("Explorer")
    .OpenSubKey("FileExts")
    .OpenSubKey(".doc")
    .OpenSubKey("OpenWithList");

var firstProgram = key.GetValue("a"); // E.g. Winword.exe

您可能希望将赋值分配为具有空检查的多个语句;-)

希望这可以帮助!

我不会给你代码,而是告诉你这些信息存储在哪里 - 我相信你可以自己弄清楚其余的:)

因此,所有数据都存储在注册表中,位于HKEY_CLASSES_ROOT .pdf为例,有一个关键的.pdf ,其中包含AcroExch.Document作为默认值(至少在我的设置上)。

同样在HKEY_CLASSES_ROOT有一个键AcroExch.Document\\Shell\\Open\\Command ,并且其中包含"C:\\Program Files (x86)\\Adobe\\Acrobat 8.0\\Acrobat\\Acrobat.exe" "%1"作为其值。 这就是我的计算机上用来打开PDF文件的内容。

暂无
暂无

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

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