确定打开给定文件扩展名的默认程序

编程入门 行业动态 更新时间:2024-10-26 22:18:24
本文介绍了确定打开给定文件扩展名的默认程序 - VBS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我找到了一些答案,但没有任何答案能准确地解决我的问题,或者与 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 00:07:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1164475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件扩展名   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!