由于日常工作中经常需要对文件路径进行拷贝,虽然文件属性页的安全选项卡中提供了完整的路径名,但毕竟需要执行两步的操作,很是不方便。通过简单的摸索,由于需要操作系统的剪切板,因此放弃了使用脚本的办法,直接编写windows程序进行拷贝。未使用资源管理器的扩展编程,因为那种做法需要使用到COM编程,而且组件本身对注册表的污染很是令人反感。
拷贝路径的Windows程序如下
#include "stdafx.h"
#include "cpyfp.h"
#include <Shellapi.h>
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow) {
LPWSTR *szArglist;
int nArgs;
szArglist = CommandLineToArgvW(GetCommandLine(), &nArgs);
if (szArglist == NULL) {
return -1;
}
if (nArgs <= 1) {
LocalFree(szArglist);
return -1;
}
if (OpenClipboard(GetDesktopWindow()) && EmptyClipboard()) {
size_t cbData = (wcslen(szArglist[1]) + 1) * sizeof(wchar_t);
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, cbData);
if (hMem == NULL) {
LocalFree(szArglist);
CloseClipboard();
return -1;
}
LPTSTR lpDest = (LPTSTR)GlobalLock(hMem);
ZeroMemory(lpDest, cbData);
memcpy_s(lpDest, cbData, szArglist[1], wcslen(szArglist[1]) * sizeof(wchar_t));
GlobalUnlock(hMem);
if (SetClipboardData(CF_UNICODETEXT, hMem) == NULL) {
LocalFree(szArglist);
CloseClipboard();
return -1;
}
CloseClipboard();
}
LocalFree(szArglist);
return 0;
}
注册表的设置如下
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\CopyFilePath]
"Icon"="C:\\Windows\\System32\\cpyfp.exe"
@="Copy full path"
[HKEY_CLASSES_ROOT\*\shell\CopyFilePath\Command]
@="\"C:\\Windows\\System32\\cpyfp.exe\" \"%1\""