#region 剪切/复制,粘贴
/// <summary>
/// 写文件到剪贴板
/// </summary>
/// <param name="cut">如果是剪切则为True,如果为复制False</param>
void CopyToClipboard(bool cut)
{
List<ZSFile> files = this.blvDirAndFileList.SelectedFiles;
if (files != null)
{
clipboardData = new DataObject(typeof(List<ZSFile>).ToString(), files);
MemoryStream memo = new MemoryStream(4);
byte[] bytes = new byte[] { (byte)(cut ? 2 : 5), 0, 0, 0 };
memo.Write(bytes, 0, bytes.Length);
clipboardData.SetData("Preferred DropEffect", memo);
Thread staThread = new Thread(new ThreadStart(this.SetClipboard));
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
staThread.Abort();
}
}
private IDataObject clipboardData;
public void SetClipboard()
{
if (clipboardData != null)
{
Clipboard.SetDataObject(clipboardData,true);
}
}
public void GetClipboardContent()
{
clipboardData = Clipboard.GetDataObject();
}
/// <summary>
/// 粘贴
/// </summary>
private void tsmiPaste_Click(object sender, EventArgs e)
{
Thread sta1Thread = new Thread(new ThreadStart(this.GetClipboardContent));
sta1Thread.SetApartmentState(ApartmentState.STA);
sta1Thread.Start();
sta1Thread.Join();
staThread.Abort();
//IDataObject clipboardData = Clipboard.GetDataObject();
if (!clipboardData.GetDataPresent(typeof(List<ZSFile>).ToString()))
return;
List<ZSFile> files = (List<ZSFile>)clipboardData.GetData(typeof(List<ZSFile>).ToString());
MemoryStream stream = (MemoryStream)clipboardData.GetData("Preferred DropEffect", true);
//int flag = stream.ReadByte();
//if (flag != 2 && flag != 5)
// return;
//bool cut = (flag == 2);
//foreach (string file in files)
//{
// string dest = homeFolder + "\\" + Path.GetFileName(file);
// try
// {
// if (cut)
// File.Move(file, dest);
// else
// File.Copy(file, dest, false);
// }
// catch (IOException ex)
// {
// MessageBox.Show(this, "Failed to perform the specified operation:\n\n" + ex.Message, "File operation failed", MessageBoxButtons.OK, MessageBoxIcon.Stop);
// }
//}
//RefreshView();
}
/// <summary>
/// 复制
/// </summary>
private void tsmiCopy_Click(object sender, EventArgs e)
{
CopyToClipboard(false);
}
/// <summary>
/// 剪切
/// </summary>
private void tsmiCut_Click(object sender, EventArgs e)
{
CopyToClipboard(true);
}
#endregion
/// <summary>
/// 写文件到剪贴板
/// </summary>
/// <param name="cut">如果是剪切则为True,如果为复制False</param>
void CopyToClipboard(bool cut)
{
List<ZSFile> files = this.blvDirAndFileList.SelectedFiles;
if (files != null)
{
clipboardData = new DataObject(typeof(List<ZSFile>).ToString(), files);
MemoryStream memo = new MemoryStream(4);
byte[] bytes = new byte[] { (byte)(cut ? 2 : 5), 0, 0, 0 };
memo.Write(bytes, 0, bytes.Length);
clipboardData.SetData("Preferred DropEffect", memo);
Thread staThread = new Thread(new ThreadStart(this.SetClipboard));
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
staThread.Abort();
}
}
private IDataObject clipboardData;
public void SetClipboard()
{
if (clipboardData != null)
{
Clipboard.SetDataObject(clipboardData,true);
}
}
public void GetClipboardContent()
{
clipboardData = Clipboard.GetDataObject();
}
/// <summary>
/// 粘贴
/// </summary>
private void tsmiPaste_Click(object sender, EventArgs e)
{
Thread sta1Thread = new Thread(new ThreadStart(this.GetClipboardContent));
sta1Thread.SetApartmentState(ApartmentState.STA);
sta1Thread.Start();
sta1Thread.Join();
staThread.Abort();
//IDataObject clipboardData = Clipboard.GetDataObject();
if (!clipboardData.GetDataPresent(typeof(List<ZSFile>).ToString()))
return;
List<ZSFile> files = (List<ZSFile>)clipboardData.GetData(typeof(List<ZSFile>).ToString());
MemoryStream stream = (MemoryStream)clipboardData.GetData("Preferred DropEffect", true);
//int flag = stream.ReadByte();
//if (flag != 2 && flag != 5)
// return;
//bool cut = (flag == 2);
//foreach (string file in files)
//{
// string dest = homeFolder + "\\" + Path.GetFileName(file);
// try
// {
// if (cut)
// File.Move(file, dest);
// else
// File.Copy(file, dest, false);
// }
// catch (IOException ex)
// {
// MessageBox.Show(this, "Failed to perform the specified operation:\n\n" + ex.Message, "File operation failed", MessageBoxButtons.OK, MessageBoxIcon.Stop);
// }
//}
//RefreshView();
}
/// <summary>
/// 复制
/// </summary>
private void tsmiCopy_Click(object sender, EventArgs e)
{
CopyToClipboard(false);
}
/// <summary>
/// 剪切
/// </summary>
private void tsmiCut_Click(object sender, EventArgs e)
{
CopyToClipboard(true);
}
#endregion