您的位置:首页 > 编程语言 > C#

C# 利用系统API 复制大文件(显示进度条)

2013-11-16 21:24 330 查看


[csharp] view
plaincopy

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.IO;

namespace MyWinfrm

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btn_select1_Click(object sender, EventArgs e)

{

OpenFileDialog openFileDialog1 = new OpenFileDialog();

if(openFileDialog1.ShowDialog()==DialogResult.OK)

{

textBox1.Text = openFileDialog1.FileName;

}

}

private void btn_select2_Click(object sender, EventArgs e)

{

FolderBrowserDialog f = new FolderBrowserDialog();

if (f.ShowDialog() == DialogResult.OK)

{

textBox2.Text = f.SelectedPath+@"\"+Path.GetFileName(textBox1.Text.Trim());

}

}

private void btn_statr_Click(object sender, EventArgs e)

{

ApiCopyFile.DoCopy(textBox1.Text.Trim(), textBox2.Text.Trim());

}

}

public class ApiCopyFile

{

private const int FO_COPY = 0x0002;

private const int FOF_ALLOWUNDO = 0x00044;

//显示进度条 0x00044 // 不显示一个进度对话框 0x0100 显示进度对话框单不显示进度条 0x0002显示进度条和对话框

private const int FOF_SILENT = 0x0002;//0x0100;

//

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto,Pack=0)]

public struct SHFILEOPSTRUCT

{

public IntPtr hwnd;

[MarshalAs(UnmanagedType.U4)]

public int wFunc;

public string pFrom;

public string pTo;

public short fFlags;

[MarshalAs(UnmanagedType.Bool)]

public bool fAnyOperationsAborted;

public IntPtr hNameMappings;

public string lpszProgressTitle;

}

[DllImport("shell32.dll", CharSet = CharSet.Auto)]

static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);

public static bool DoCopy(string strSource, string strTarget)

{

SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();

fileop.wFunc = FO_COPY;

fileop.pFrom = strSource;

fileop.lpszProgressTitle = "复制大文件";

fileop.pTo = strTarget;

//fileop.fFlags = FOF_ALLOWUNDO;

fileop.fFlags = FOF_SILENT;

return SHFileOperation(ref fileop)==0;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: