如何直接开启一个文件

来源:岁月联盟 编辑:zhu 时间:2007-01-31
   

  Shell 是用来执行外部的可执行文件
如果是一个文件,那要用ShellExecute来启动预设的程式来开启文件
'HINSTANCE ShellExecute(
' HWND   hwnd,     // handle to parent window
' LPCTSTR lpOperation, // pointer to string that specifies operation to perform
' LPCTSTR lpFile,    // pointer to filename string
' LPTSTR  lpParameters, // pointer to string that specifies executable-file parameters
' LPCTSTR lpDirectory, // pointer to string that specifies default directory
' INT   nShowCmd   // whether file is shown when opened
'  );
例:.doc 是由word或wordpad来开启
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
   (ByVal hwnd As Long, ByVal lpOperation As String, _
   ByVal lpFile As String, ByVal lpParameters As String, _
   ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOW = 5
ret = ShellExecute(me.hWnd, "open" , "文件完整路径及名称","", "", SW_SHOW

  事实上有一个更快的方式,便是使用Start指令,这个指令用法简单

  eg. Start mydoc.doc

  故,在vb中便是使用 Shell "start mydoc.doc"

  如何查看某个文件是和谁相关联呢?例如:.txt是由哪个程式开启,

  1.查[HKEY_CLASSES_ROOT/.txt]

  取预设值,如本人电脑预设值为 "txtfile"

  2.查[HKEY_CLASSES_ROOT/txtfile/shell/open/command]

  取预设值,如本人电脑预设值为 "C:/WINDOWS/NOTEPAD.EXE %1"

  如此可知.txt 是内定由NotePad.exe所执行。

  注:若step 1.取得的预设值是 "xxxx",则step 2.便是查

  [HKEY_CLASSES_ROOT/xxxx/shell/open/command]的预设值

[责任编辑:editor]