如何通过扫描注册表找到与某个文件关联的程序并打开这个文件?

时间:2022-09-05 00:02:02
是一个C++控制台程序.

比如输入命令:type test.doc
程序先搜索与*.doc文件建立关联关系的程序,然后用这个程序打开此文件
如果没有程序与它建立关联就用记事本打开。

这要怎么实现?
望大家指点一下。。

7 个解决方案

#1


调用外部程序很简单就可以实现。
参考我制作OOo绿色版的方法:
@echo off

ftype odtfile="%CD%\program\swriter.exe" -o "%%1"
reg add HKCR\odtfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,1" /f
assoc .odt=odtfile

ftype ottfile="%CD%\program\swriter.exe" -n "%%1"
reg add HKCR\ottfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,2" /f
assoc .ott=ottfile

ftype odsfile="%CD%\program\scalc.exe" -o "%%1"
reg add HKCR\odsfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,3" /f
assoc .ods=odsfile

ftype otsfile="%CD%\program\scalc.exe" -n "%%1"
reg add HKCR\otsfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,4" /f
assoc .ots=otsfile

ftype odgfile="%CD%\program\sdraw.exe" -o "%%1"
reg add HKCR\odgfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,5" /f
assoc .odg=odgfile

ftype otgfile="%CD%\program\sdraw.exe" -n "%%1"
reg add HKCR\otgfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,6" /f
assoc .otg=otgfile

ftype odpfile="%CD%\program\simpress.exe" -o "%%1"
reg add HKCR\odpfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,7" /f
assoc .odp=odpfile

ftype otpfile="%CD%\program\simpress.exe" -n "%%1"
reg add HKCR\otpfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,8" /f
assoc .otp=otpfile

if exist "%CD%\OpenOffice.url" del /Q "%CD%\OpenOffice.url"

set path=%CD%\program\soffice.exe
set result="%CD%\OpenOffice.url"
echo [InternetShortcut] >> %result%
echo URL="%path%" >> %result%
echo IconIndex=0 >> %result%
echo IconFile=%path% >> %result%

call !restore.bat

#2


其中,assoc 程序就是用来关联的。ftype 注册文件类型的。
具体用法你查一下google
另,C++在使用时,只需要ShellExecute或WinExec或CreateProcess就好。

#3


http://www.cppblog.com/bidepan2023/archive/2007/07/20/28419.aspx

#4


那要怎么才能判断一个文件是否已经有程序与其关联了呢?

#5


使用

ShellExecute/ShellExecuteEx来由系统来调用某文件相关的程序来打开文档。

所有的文件关联都在HKEY—CLASSES—ROOT下面,要实现文件关联必须在HKEY—CLASSES—ROOT中增加两个键值,一是和文件扩展名对应的类型说明,二是对这种类型文件的操作所需要执行的应用程序。

打开注册表,你去看这个HKEY—CLASSES—ROOT键展开,你就会发现到处是扩展名。

在查找文件关联的时候,只要查找该键下有没有相关的文件扩展名的键值,如果没有,表明没有文件关联,如果有,则进入去查看它所关联的应用程序就可以了。

关于注册表操作:

Registry functions

#6


引用 4 楼 wxgiter 的回复:
那要怎么才能判断一个文件是否已经有程序与其关联了呢?


查看ShellExecute的返回值是否为SE_ERR_NOASSOC...

#7


谢谢大家

#1


调用外部程序很简单就可以实现。
参考我制作OOo绿色版的方法:
@echo off

ftype odtfile="%CD%\program\swriter.exe" -o "%%1"
reg add HKCR\odtfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,1" /f
assoc .odt=odtfile

ftype ottfile="%CD%\program\swriter.exe" -n "%%1"
reg add HKCR\ottfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,2" /f
assoc .ott=ottfile

ftype odsfile="%CD%\program\scalc.exe" -o "%%1"
reg add HKCR\odsfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,3" /f
assoc .ods=odsfile

ftype otsfile="%CD%\program\scalc.exe" -n "%%1"
reg add HKCR\otsfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,4" /f
assoc .ots=otsfile

ftype odgfile="%CD%\program\sdraw.exe" -o "%%1"
reg add HKCR\odgfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,5" /f
assoc .odg=odgfile

ftype otgfile="%CD%\program\sdraw.exe" -n "%%1"
reg add HKCR\otgfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,6" /f
assoc .otg=otgfile

ftype odpfile="%CD%\program\simpress.exe" -o "%%1"
reg add HKCR\odpfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,7" /f
assoc .odp=odpfile

ftype otpfile="%CD%\program\simpress.exe" -n "%%1"
reg add HKCR\otpfile\DefaultIcon /ve /t Reg_SZ /d "%CD%\program\soffice.exe,8" /f
assoc .otp=otpfile

if exist "%CD%\OpenOffice.url" del /Q "%CD%\OpenOffice.url"

set path=%CD%\program\soffice.exe
set result="%CD%\OpenOffice.url"
echo [InternetShortcut] >> %result%
echo URL="%path%" >> %result%
echo IconIndex=0 >> %result%
echo IconFile=%path% >> %result%

call !restore.bat

#2


其中,assoc 程序就是用来关联的。ftype 注册文件类型的。
具体用法你查一下google
另,C++在使用时,只需要ShellExecute或WinExec或CreateProcess就好。

#3


http://www.cppblog.com/bidepan2023/archive/2007/07/20/28419.aspx

#4


那要怎么才能判断一个文件是否已经有程序与其关联了呢?

#5


使用

ShellExecute/ShellExecuteEx来由系统来调用某文件相关的程序来打开文档。

所有的文件关联都在HKEY—CLASSES—ROOT下面,要实现文件关联必须在HKEY—CLASSES—ROOT中增加两个键值,一是和文件扩展名对应的类型说明,二是对这种类型文件的操作所需要执行的应用程序。

打开注册表,你去看这个HKEY—CLASSES—ROOT键展开,你就会发现到处是扩展名。

在查找文件关联的时候,只要查找该键下有没有相关的文件扩展名的键值,如果没有,表明没有文件关联,如果有,则进入去查看它所关联的应用程序就可以了。

关于注册表操作:

Registry functions

#6


引用 4 楼 wxgiter 的回复:
那要怎么才能判断一个文件是否已经有程序与其关联了呢?


查看ShellExecute的返回值是否为SE_ERR_NOASSOC...

#7


谢谢大家