Windows 确定打开给定文件扩展名的默认程序-VBS
我已经找到了一些答案,但没有一个能确切地解决我的问题,或者说不能解决VBS问题 在提供特定文件扩展名时,我正在寻找一种方法来确定默认程序的完整路径 我的最终目标是自动创建程序打开“.DOC”文件(通常是MS Word)的快捷方式。但这在不同的windows机器上显然会有所不同 我想做一些事情,比如: strDefaultDOCProgram=WshShell.FindAssociatedProgram(“doc”) 在哪里 strDefaultDOCProgram=“C:\Program Files\Microsoft Office 15\root\office15\winword.exe” 也许有帮助? 使用assocWindows 确定打开给定文件扩展名的默认程序-VBS,windows,vbscript,Windows,Vbscript,我已经找到了一些答案,但没有一个能确切地解决我的问题,或者说不能解决VBS问题 在提供特定文件扩展名时,我正在寻找一种方法来确定默认程序的完整路径 我的最终目标是自动创建程序打开“.DOC”文件(通常是MS Word)的快捷方式。但这在不同的windows机器上显然会有所不同 我想做一些事情,比如: strDefaultDOCProgram=WshShell.FindAssociatedProgram(“doc”) 在哪里 strDefaultDOCProgram=“C:\Program Fil
assoc /?
Displays or modifies file extension associations
ASSOC [.ext[=[fileType]]]
.ext Specifies the file extension to associate the file type with
fileType Specifies the file type to associate with the file extension
和ftype
type /?
isplays or modifies file types used in file extension associations
TYPE [fileType[=[openCommandString]]]
fileType Specifies the file type to examine or change
openCommandString Specifies the open command to use when launching files
of this type.
像
通过使用.Exec执行这些程序的脚本
更新:
从命令中剪切文件等级库:
>> sCmd = """C:\Program Files\OpenOffice.org 3\program\\swriter.exe"" -o ""%1"""
>> WScript.Echo sCmd
>> WScript.Echo Split(sCmd, """")(1)
>>
"C:\Program Files\OpenOffice.org 3\program\\swriter.exe" -o "%1"
C:\Program Files\OpenOffice.org 3\program\\swriter.exe
更新II:
不要使用。重新研磨以尝试在本周版本的注册表中查找信息;assoc和ftype是操作系统为您的问题提供的工具。打开文件的方式随着时间的推移而改变。您谈论的是打开文件的传统方式,这仍然是最常见的方式 开始 要支持office,您可以输入win.ini*.doc=c:\winword.exe 关联为每用户和每台机器,每用户设置覆盖机器设置 在NT/Win 95中,它被扩展。因此,HKCR.ext可以保存打开的字符串(\shell\open),以便与win.ini兼容,但更典型的是指向一个文件类,例如HKCR.txt=txtfile。通过查找HKCR\txtfile\shell\open,可以获得以下命令 由于程序窃取文件关联,现在有一层其他关联放在上面。因此,该命令是从上面和这些较新的键HKEY\U CLASSES\U ROOT\SystemFileAssociations(其中还包括一般类型文件的较新概念的关联-图片或音乐)或HKEY\U CURRENT\U USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts构建的 对于Word,它是使用DDE(也在上面的注册表项中指定)打开的,而不仅仅是命令行。也就是说,它作为DDE服务器启动,然后向其发送带有要打开的文件名的fileopen命令 打开文件的新方法 现在使用COM打开文件。程序在上述键下注册IDropTarget 上下文菜单处理程序可以覆盖上述内容。他们也在上面注册
最好的方法是shellexec文件。它会像双击一样打开。我最终决定使用
assoc
和ftype
命令,以防我们想在任何其他Windows版本上使用此脚本。这里有一个函数,可以完成我所需要的一切。我希望这对别人有帮助
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
我会玩这个,但似乎有希望!有没有办法只获取路径而不添加任何标志?谢谢!我想+1,但还不能;)使用操作系统中的内置工具无疑是更好的实践,更具可扩展性。然而,在我的例子中,使用
.regrad
比从assoc
和ftype
捕获输出,然后无论如何都要拆分输出要短得多,也容易得多。我只需要在Windows 7机器上使用这个工具,但是如果其他人有更大的样本量,那么他们可能应该使用内置工具。我是这样使用的:Dim WshShell,strProg,strProgPath Set WshShell=CreateObject(“WScript.Shell”)strProg=WshShell.regrad(“HKCR\.doc\”)strProgPath=WshShell.regrad(“HKCR\”&strProg&“\Protocol\StdFileEditing\Server\”
下面是使用Win API的另一种方法:
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