我有一个使用Clipboard类的Windows窗体应用程序。我的老板要我把它变成一个可以在后台运行的Windows服务。我得到的服务不应该与桌面交互,但在这种情况下,成为一个Windows服务是有道理的。
protected override void WndProc(ref System.Windows.Forms.Message m)
{
// defined in winuser.h
const int WM_DRAWCLIPBOARD = 0x308;
const int WM_CHANGECBCHAIN = 0x030D;
switch(m.Msg)
{
case WM_DRAWCLIPBOARD:
DisplayClipboardData();
SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
break;
case WM_CHANGECBCHAIN:
if (m.WParam == nextClipboardViewer)
nextClipboardViewer = m.LParam;
else
SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
break;
default:
base.WndProc(ref m);
break;
}
}
void DisplayClipboardData()
{
try
{
IDataObject iData = new DataObject();
iData = Clipboard.GetDataObject();
System.Collections.Specialized.StringCollection returnList = null;
string entireCB = null;
if (iData.GetDataPresent(DataFormats.FileDrop))
{
if (Clipboard.ContainsFileDropList())
{
returnList = Clipboard.GetFileDropList();
// Clipboard.SetFileDropList(replacementList);
}
// return returnList;
}
if (returnList != null)
{
foreach (string item in returnList)
{
entireCB = entireCB + "\"" + item + "\",\"" + DateTime.Now.ToString("MM/dd/yyyy h:mm tt") + "\"," + Environment.NewLine;
}
System.IO.File.WriteAllText(@"C:\temp\CopiedFiles.csv", entireCB);
//System.IO.File.Open(@"C:\temp\CopiedFiles.txt", System.IO.FileMode.Open);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
编辑:我们这样做是因为我们想捕获试图移动机密数据的人的文件副本。