欢迎来到山村网

PowerShell实现查询打开某个文件的默认应用程序

2019-03-02 13:46:54浏览:842 来源:山村网   
核心摘要:  这篇文章主要介绍了PowerShell实现查询打开某个文件的默认应用程序,本文通过C#调用WindowsAPI来实现这个需求,需要的朋友可以

  这篇文章主要介绍了PowerShell实现查询打开某个文件的默认应用程序,本文通过C#调用Windows API来实现这个需求,需要的朋友可以参考下

  许多文件扩展名和一个可执行应用程序绑定。正因为这样你才可以使用Invoke-Item打开一个文档。

  要找出一个给定后缀名的文件是由那个默认引用程序打开它,并不麻烦。我们可以使用Windows系统中的注册表,自行编程解决。但是在扫描注册表时,要稍微留意一下32位和64位机器的问题,这不是本文重点,点到为止。

  另外一种途径,稍显旁门左道,调用Windows API。下面的例子会演示如何调用。采取这种途径最大的优势是借力于操作系统。而你的付出成本只是用C#代码间接调用Windows API中的函数而已:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 $Source = @" using System; using System.Text; using System.Runtime.InteropServices; public class Win32API { [Dllimport("shell32.dll", EntryPoint="FindExecutable")] public static extern long FindExecutableA(string lpFile, string lpDirectory, StringBuilder lpResult); public static string FindExecutable(string pv_strFilename) { StringBuilder objResultBuffer = new StringBuilder(1024); long lngResult = 0; lngResult = FindExecutableA(pv_strFilename, string.Empty, objResultBuffer); if(lngResult >= 32) { return objResultBuffer.ToString(); } return string.Format("Error: ({0})", lngResult); } } "@ Add-Type -TypeDefinition $Source -ErrorAction SilentlyContinue $FullName = 'c:Windowswindowsupdate.log' $Executable = [Win32API]::FindExecutable($FullName) "$FullName will be launched by $Executable"

  唯一有个限制,就是FindExecutable()需要检查的文件是存在的,你不能只用文件扩展名去请求。

  另外@reidca反馈说该方法不能检测MMC加载项打开的文件,比如cer和pfx证书文件,程序会崩溃。

(责任编辑:豆豆)
下一篇:

Python中for循环控制语句用法实例

上一篇:

WebCruiser的使用方法

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com