玖色琉璃-轮回 2019-08-15 17:25 采纳率: 0%
浏览 424
已结题

VB 怎么在多选文件后,右键菜单打开程序为只运行一个EXE?

我自己写了注册表,让鼠标右键点击一个文件时,增加了一项“我的应用”到ContextMenu。
现在我的问题是,我选中一个文件,这样右键操作,则运行“我的应用“一次。
但是,如果我多选文件,再这样右键操作,则会运行”我的应用“N次。
我想问,有没有办法能在我多选的时候,让”我的应用“只运行一次且获得我多选的所有文件的命令行参数(这个文件的绝对路径)。求VB解决方案,最好有代码实例,万分感谢!
我自己已经查了很多,比如shell拓展,进程间通信,但是可能是我悟性不够,没太懂。
上面表述也许不够清楚,具体想实现的例子就是像 压缩软件 在多选文件压缩的时候的那个”添加到压缩文件“的效果。
图片说明

  • 写回答

1条回答

  • threenewbee 2019-08-16 00:11
    关注

    stackoverflow上有人给出一个办法

    编写一个vbs

    set WshShell = WScript.CreateObject("WScript.Shell")
    set WMIService = GetObject("winmgmts:root\cimv2")
    
    dim FirstCommandLine, CommandLineParts, AllFiles
    
    set AllInstances1 = WMIService.ExecQuery("Select CommandLine FROM Win32_Process WHERE CommandLine LIKE '%" & wscript.scriptname & "%'")
    For Each item In AllInstances1
        FirstCommandLine = item.CommandLine 'Get the command line of the first instance of this script
        exit for
    Next
    
    if InStr(FirstCommandLine,WScript.Arguments.Unnamed(0)) then 'This is the first instance
        WScript.Sleep 400
        'Update instance list to check if other instances appeared
        set AllInstances2 = WMIService.ExecQuery("Select CommandLine FROM Win32_Process WHERE CommandLine LIKE '%" & wscript.scriptname & "%'")
        while AllInstances2.count > AllInstances1.count 'While other instances keep appearing
            WScript.Sleep 400
            'Keep updating instance list
            set AllInstances1 = AllInstances2
            set AllInstances2 = WMIService.ExecQuery("Select CommandLine FROM Win32_Process WHERE CommandLine LIKE '%" & wscript.scriptname & "%'")
        wend
        For Each item In AllInstances2
            CommandLineParts = Split(item.CommandLine,"""")
            AllFiles = AllFiles & """" & CommandLineParts(UBound(CommandLineParts)-1) & """ " 'Get all the file paths
        Next
        WshShell.Run("""Path\to your\application.exe"" /switches" & AllFiles) '这里是你实际要调用的用来打开文件的VB程序。用Command语句和Split得到文件
    else 'This is not the first instance. Before exiting, sleep for a moment for the first one to detect.
        WScript.Sleep 4000
    end if
    

    注册表中 wscript //nologo "Full Path\myscript.vbs" "%1" 作为命令行(Full Path\myscript.vbs是这个脚本的路径)

    具体看 https://stackoverflow.com/questions/32107534/more-about-opening-multiple-files-through-the-context-menu

    这里还有一个基于消息通讯的
    https://stackoverflow.com/questions/21062046/open-multiple-files-via-shell-context-menu-as-params
    不过是delphi写的,思路我看了下,VB应该也可以实现。

    评论

报告相同问题?

悬赏问题

  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题