确定打开给定文件扩展名的默认程序 - VBS [英] Determine default program to open given file extension - VBS

查看:26
本文介绍了确定打开给定文件扩展名的默认程序 - VBS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一些答案,但没有任何答案能准确地解决我的问题,或者与 VBS 无关.

There are a handful of answers I've found but nothing that deals specifically with my problem exactly, or not with VBS.

在提供特定文件扩展名时,我正在寻找一种方法来确定默认程序的完整路径.

I am looking for a way to determine the full path to a the default program when providing a specific file extension.

我的最终目标是自动创建任何程序打开.DOC"文件(通常是 MS Word)的快捷方式.但这在不同的 Windows 机器上显然会有所不同.

My ultimate goal is to automatically create a shortcut to whatever program opens ".DOC" files (typically MS Word). But this will obviously vary on different windows machines.

我想做类似的事情:

strDefaultDOCProgram = WshShell.FindAssociatedProgram("doc")

strDefaultDOCProgram = WshShell.FindAssociatedProgram("doc")

哪里

strDefaultDOCProgram = "C:\Program Files\Microsoft Office 15\root\office15\winword.exe"

strDefaultDOCProgram = "C:\Program Files\Microsoft Office 15\root\office15\winword.exe"

也许有帮助?询问 Windows 7 - 默认情况下哪个程序打开此文件

推荐答案

我最终决定使用 assocftype 命令,以防万一我们想使用这个脚本在任何其他 Windows 版本上.这是一个可以完成我需要的所有功能的函数.我希望它对某人有帮助!

I ultimately decided to use the assoc and ftype commands just in case we want to use this script on any other Windows versions. Here's a function that will do everything I needed. I hope it's helpful to someone!

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")

' Should supply the program extension with period "." included
Function GetProgramPath(ext)
Dim strProg, strProgPath

' Get Program Association Handle
Set oExec =  WshShell.Exec("cmd.exe /c assoc " & ext)
strProg = oExec.StdOut.ReadLine()
strProg = Split(strProg, "=")(1)

' Get Path To Program
Set oExec =  WshShell.Exec("cmd.exe /c ftype " & strProg)
strProgPath = oExec.StdOut.ReadLine()
strProgPath = Split(strProgPath, """")(1)

' Return the program path
GetProgramPath = strProgPath

End Function

strPath = GetProgramPath(".doc")
WScript.Echo strPath

这篇关于确定打开给定文件扩展名的默认程序 - VBS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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