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

[csharp]   view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Runtime.InteropServices;  
  10. using System.IO;  
  11.   
  12. namespace MyWinfrm  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         private void btn_select1_Click(object sender, EventArgs e)  
  22.         {  
  23.             OpenFileDialog openFileDialog1 = new OpenFileDialog();  
  24.             if(openFileDialog1.ShowDialog()==DialogResult.OK)  
  25.             {  
  26.                 textBox1.Text = openFileDialog1.FileName;  
  27.             }  
  28.         }  
  29.   
  30.         private void btn_select2_Click(object sender, EventArgs e)  
  31.         {  
  32.             FolderBrowserDialog f = new FolderBrowserDialog();  
  33.             if (f.ShowDialog() == DialogResult.OK)  
  34.             {  
  35.                 textBox2.Text = f.SelectedPath+@"\"+Path.GetFileName(textBox1.Text.Trim());  
  36.             }  
  37.         }  
  38.   
  39.         private void btn_statr_Click(object sender, EventArgs e)  
  40.         {  
  41.            ApiCopyFile.DoCopy(textBox1.Text.Trim(), textBox2.Text.Trim());   
  42.         }  
  43.     }  
  44.     public class ApiCopyFile  
  45.     {  
  46.         private const int FO_COPY = 0x0002;  
  47.         private const int FOF_ALLOWUNDO = 0x00044;  
  48.         //显示进度条  0x00044 // 不显示一个进度对话框 0x0100 显示进度对话框单不显示进度条  0x0002显示进度条和对话框  
  49.         private const int FOF_SILENT = 0x0002;//0x0100;  
  50.         //  
  51.         [StructLayout(LayoutKind.Sequential,   CharSet = CharSet.Auto,Pack=0)]    
  52.         public struct SHFILEOPSTRUCT    
  53.         {  
  54.             public IntPtr hwnd;    
  55.             [MarshalAs(UnmanagedType.U4)]    
  56.             public int wFunc;    
  57.             public string pFrom;   
  58.             public string pTo;   
  59.             public short fFlags;    
  60.             [MarshalAs(UnmanagedType.Bool)]    
  61.             public bool fAnyOperationsAborted;   
  62.             public IntPtr hNameMappings;   
  63.             public string lpszProgressTitle;    
  64.         }  
  65.         [DllImport("shell32.dll",   CharSet = CharSet.Auto)]   
  66.         static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);    
  67.         public static bool DoCopy(string strSource, string strTarget)   
  68.         {   
  69.             SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();  
  70.             fileop.wFunc = FO_COPY;    
  71.             fileop.pFrom = strSource;  
  72.             fileop.lpszProgressTitle = "复制大文件";  
  73.             fileop.pTo = strTarget;   
  74.             //fileop.fFlags = FOF_ALLOWUNDO;  
  75.             fileop.fFlags = FOF_SILENT;  
  76.             return  SHFileOperation(ref  fileop)==0;   
  77.         }   
  78.     }  
  79. }  

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值