• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

Finding the Associated Program

发布: 2007-6-21 21:57 | 作者:   | 来源:   | 查看: 16次 | 进入软件测试论坛讨论

领测软件测试网

   
  Finding the Associated Program

Looking to find the executable for a particular file extension? This code snippet has the answer.

Just pass it the extension - such as TXT or MDB - and it注释:ll return the associated Windows program.

It works by using the FindExecutable API call to locate the executable. However this requires an actual file with the requested extension, which is created with another couple of calls. The temporary file is deleted after the process has finished.

To use this code, call GetAssociatedExecutable, passing in the extension as a string.

Usage

x = GetAssociatedExecutable("MDB")Code

Private Declare Function FindExecutable Lib _
    "shell32.dll" Alias "FindExecutableA" _
    (ByVal lpFile As String, ByVal lpDirectory _
    As String, ByVal lpResult As String) As Long

Private Declare Function GetTempFileName Lib _
    "kernel32" Alias "GetTempFileNameA" (ByVal _
    lpszPath As String, ByVal lpPrefixString _
    As String, ByVal wUnique As Long, ByVal _
    lpTempFileName As String) As Long

Private Declare Function GetTempPath Lib _
    "kernel32" Alias "GetTempPathA" (ByVal _
    nBufferLength As Long, ByVal lpBuffer As _
    String) As Long

Public Function GetAssociatedExecutable(ByVal _
    Extension As String) As String

    Dim Path As String
    Dim FileName As String
    Dim nRet As Long
    Const MAX_PATH As Long = 260
    
    注释:Create a tempfile
    Path = String$(MAX_PATH, 0)
    
    If GetTempPath(MAX_PATH, Path) Then
        FileName = String$(MAX_PATH, 0)
    
        If GetTempFileName(Path, "~", 0, FileName) Then
            FileName = Left$(FileName, _
                InStr(FileName, vbNullChar) - 1)
        
            注释:Rename it to use supplied extension
            Name FileName As Left$(FileName, _
                InStr(FileName, ".")) & Extension
                FileName = Left$(FileName, _
                InStr(FileName, ".")) & Extension
        
            注释:Get name of associated EXE
            Path = String$(MAX_PATH, 0)
        
            Call FindExecutable(FileName, _
                vbNullString, Path)
            GetAssociatedExecutable = Left$( _
                Path, InStr(Path, vbNullChar) - 1)
        
            注释:Clean up
            Kill FileName
        
        End If
    
    End If

End Function

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网