如何在 Windows 资源管理器中从剪贴板获取文件路径?
How to Get the File Path from Clipboard in Windows Explorer?
我尝试在 Windows 资源管理器中从剪贴板(复制的文件)获取文件路径。
我写了:
Set Ws = CreateObject("WScript.Shell")
MsgBox Ws.ClipBoard
但它指出错误:
Error: Object doesn't support this property or method: 'Ws.ClipBoard'
我有很多获取剪贴板数据的方法,但只与 Internet Explorer 相关,与 windows Explorer 文件路径相关。
这展示了如何使用 HDrop。 VBScript 很容易移植到 VB.NET(将所有参数括在括号中并使所有内容变暗 As Object
)。
REM PrintClip.bat #2 Also does filenames on the clipboard
REM This file compiles PrintClip.vb to PrintClip.exe
REM PrintClip.exe prints any text or filenames on the clipboard to a console
REM To use
REM PrintClip
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\PrintClip.exe" "%~dp0\PrintClip.vb" /verbose
pause
'PrintClip.vb #2
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Public Module PrintClip
Public Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Integer
Public Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As IntPtr
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As IntPtr) As Integer
Public Declare Function CloseClipboard Lib "user32" () As Integer
Public Declare UNICODE Function DragQueryFileW Lib "shell32.dll" (ByVal HDROP As Integer, ByVal UINT As Integer, ByVal lpStr As String, ByVal ch As Integer) As Integer
Public Const CF_TEXT = 1
Public Const CF_BITMAP = 2
Public Const CF_METAFILEPICT = 3
Public Const CF_SYLK = 4
Public Const CF_DIF = 5
Public Const CF_TIFF = 6
Public Const CF_OEMTEXT = 7
Public Const CF_DIB = 8
Public Const CF_PALETTE = 9
Public Const CF_PENDATA = 10
Public Const CF_RIFF = 11
Public Const CF_WAVE = 12
Public Const CF_UNICODETEXT = 13
Public Const CF_ENHMETAFILE = 14
Public Const CF_HDROP = 15
Public Const CF_OWNERDISPLAY = &H80
Public Const CF_DSPTEXT = &H81
Public Const CF_DSPBITMAP = &H82
Public Const CF_DSPMETAFILEPICT = &H83
Public Const CF_DSPENHMETAFILE = &H8E
Sub Main()
' On Error Resume Next
Dim Ret as IntPtr
If OpenClipboard(0) <> 0 then
If IsClipboardFormatAvailable(CF_UNICODETEXT) <> 0 then
Ret = GetClipboardData( CF_UNICODETEXT)
Console.writeline(Marshal.PtrToStringUni(Ret))
Environment.ExitCode = 0
ElseIf IsClipboardFormatAvailable(CF_hDrop) <> 0 then
Dim TotalCount as Integer
Dim FName as String
Dim hDrop as IntPtr
hDrop = GetClipboardData( CF_hDrop)
FName = Space(33000)
TotalCount = DragQueryFileW(hDrop, &hFFFFFFFF, FName, 33000)
For x = 0 to TotalCount - 1
FName = Space(33000)
If DragQueryFileW(hDrop, x, FName, 33000) <> 0 then
Console.writeline(Trim(FName))
End If
Next
Environment.ExitCode = 0
Else
Environment.ExitCode = 1
Console.writeline("No text or filenames on clipboard")
End If
CloseClipboard()
Else
Environment.ExitCode = err.lastdllerror
Console.Writeline("Clipboard is locked by another application")
End If
End Sub
End Module
我尝试在 Windows 资源管理器中从剪贴板(复制的文件)获取文件路径。
我写了:
Set Ws = CreateObject("WScript.Shell")
MsgBox Ws.ClipBoard
但它指出错误:
Error: Object doesn't support this property or method: 'Ws.ClipBoard'
我有很多获取剪贴板数据的方法,但只与 Internet Explorer 相关,与 windows Explorer 文件路径相关。
这展示了如何使用 HDrop。 VBScript 很容易移植到 VB.NET(将所有参数括在括号中并使所有内容变暗 As Object
)。
REM PrintClip.bat #2 Also does filenames on the clipboard
REM This file compiles PrintClip.vb to PrintClip.exe
REM PrintClip.exe prints any text or filenames on the clipboard to a console
REM To use
REM PrintClip
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\PrintClip.exe" "%~dp0\PrintClip.vb" /verbose
pause
'PrintClip.vb #2
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Public Module PrintClip
Public Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Integer
Public Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As IntPtr
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As IntPtr) As Integer
Public Declare Function CloseClipboard Lib "user32" () As Integer
Public Declare UNICODE Function DragQueryFileW Lib "shell32.dll" (ByVal HDROP As Integer, ByVal UINT As Integer, ByVal lpStr As String, ByVal ch As Integer) As Integer
Public Const CF_TEXT = 1
Public Const CF_BITMAP = 2
Public Const CF_METAFILEPICT = 3
Public Const CF_SYLK = 4
Public Const CF_DIF = 5
Public Const CF_TIFF = 6
Public Const CF_OEMTEXT = 7
Public Const CF_DIB = 8
Public Const CF_PALETTE = 9
Public Const CF_PENDATA = 10
Public Const CF_RIFF = 11
Public Const CF_WAVE = 12
Public Const CF_UNICODETEXT = 13
Public Const CF_ENHMETAFILE = 14
Public Const CF_HDROP = 15
Public Const CF_OWNERDISPLAY = &H80
Public Const CF_DSPTEXT = &H81
Public Const CF_DSPBITMAP = &H82
Public Const CF_DSPMETAFILEPICT = &H83
Public Const CF_DSPENHMETAFILE = &H8E
Sub Main()
' On Error Resume Next
Dim Ret as IntPtr
If OpenClipboard(0) <> 0 then
If IsClipboardFormatAvailable(CF_UNICODETEXT) <> 0 then
Ret = GetClipboardData( CF_UNICODETEXT)
Console.writeline(Marshal.PtrToStringUni(Ret))
Environment.ExitCode = 0
ElseIf IsClipboardFormatAvailable(CF_hDrop) <> 0 then
Dim TotalCount as Integer
Dim FName as String
Dim hDrop as IntPtr
hDrop = GetClipboardData( CF_hDrop)
FName = Space(33000)
TotalCount = DragQueryFileW(hDrop, &hFFFFFFFF, FName, 33000)
For x = 0 to TotalCount - 1
FName = Space(33000)
If DragQueryFileW(hDrop, x, FName, 33000) <> 0 then
Console.writeline(Trim(FName))
End If
Next
Environment.ExitCode = 0
Else
Environment.ExitCode = 1
Console.writeline("No text or filenames on clipboard")
End If
CloseClipboard()
Else
Environment.ExitCode = err.lastdllerror
Console.Writeline("Clipboard is locked by another application")
End If
End Sub
End Module