• 大小: 40KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-12
  • 语言: C#
  • 标签: 异步  

资源简介

C#实现异步拷贝多个文件到指定的目录,以及数据线程访问ui线程控件的方法。

资源截图

代码片段和文件信息

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.IO;
using System.Threading;

namespace DelegateTest
{
    public partial class Form1 : Form
    {
        FileStream fs = null;
        
        public Form1()
        {
            InitializeComponent();
   
        }

        private void button1_Click(object sender EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.title = “选定文件“;
            dialog.Multiselect = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string[] filenames = dialog.FileNames;
                foreach(string str in filenames)
                {
                    string realname =str.Substring( str.LastIndexOf(‘\\‘)+1);
                    this.comboBox1.Items.Add(new ComboboxItem(realnamestr));

                }
                this.comboBox1.SelectedIndex = 0;
            }
        }

        private void button2_Click(object sender EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowNewFolderButton = true;
            
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = dialog.SelectedPath;
            }
        }

        private void Form1_Load(object sender EventArgs e)
        {
            Rectangle rect = System.Windows.Forms.SystemInformation.WorkingArea;
            this.Left = (rect.Width - this.Width) / 2;
            this.Top = (rect.Height - this.Height) / 2;
        }

        public delegate bool CopyFileToDesPathEventHadler(string[] filenamestring path);
        private bool CopyFileToDesPath(string[] filenamestring path) 
        {
            foreach(string str in filename)
            {
                try
                {
                    FileStream fr = new FileStream(strFileMode.OpenFileAccess.Read);
                    string realname=str.Substring( str.LastIndexOf(‘\\‘)+1);
                    FileStream fw = new FileStream(path+“\\“+realnameFileMode.CreateFileAccess.Write);
                    byte[] buffer = new byte[1024];
                    int length = fr.Read(buffer0buffer.Length);
                    while(length!=0)
                    {
                        fw.Write(buffer0length);
                        fw.Flush();
                        length = fr.Read(buffer0buffer.Length);
                    }
                    fr.Close();
                    fw.Close();
                    this.SetListBox(str);
                    Thread.Sleep(2000);
                }
                catch (Exception e) {
                    MessageBox.Show(e.ToString());
                }
            }
            return false;
        }
        publ

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      12288  2012-08-02 14:44  DelegateTest\bin\Debug\DelegateTest.exe

     文件      28160  2012-08-02 14:44  DelegateTest\bin\Debug\DelegateTest.pdb

     文件      14328  2012-08-06 17:45  DelegateTest\bin\Debug\DelegateTest.vshost.exe

     文件        490  2009-06-11 05:14  DelegateTest\bin\Debug\DelegateTest.vshost.exe.manifest

     文件       3770  2012-08-01 17:50  DelegateTest\DelegateTest.csproj

     文件       4586  2012-08-02 14:44  DelegateTest\Form1.cs

     文件       6509  2012-08-02 14:33  DelegateTest\Form1.Designer.cs

     文件       5814  2012-08-02 14:33  DelegateTest\Form1.resx

     文件        607  2012-08-06 17:45  DelegateTest\obj\Debug\DelegateTest.csproj.FileListAbsolute.txt

     文件        847  2012-08-02 14:33  DelegateTest\obj\Debug\DelegateTest.csproj.GenerateResource.Cache

     文件      12288  2012-08-02 14:44  DelegateTest\obj\Debug\DelegateTest.exe

     文件        180  2012-08-02 14:33  DelegateTest\obj\Debug\DelegateTest.Form1.resources

     文件      28160  2012-08-02 14:44  DelegateTest\obj\Debug\DelegateTest.pdb

     文件        180  2012-08-01 17:50  DelegateTest\obj\Debug\DelegateTest.Properties.Resources.resources

     文件        493  2012-08-01 17:46  DelegateTest\Program.cs

     文件       1356  2012-08-01 17:46  DelegateTest\Properties\AssemblyInfo.cs

     文件       2874  2012-08-01 17:46  DelegateTest\Properties\Resources.Designer.cs

     文件       5612  2012-08-01 17:46  DelegateTest\Properties\Resources.resx

     文件       1097  2012-08-01 17:46  DelegateTest\Properties\Settings.Designer.cs

     文件        249  2012-08-01 17:46  DelegateTest\Properties\Settings.settings

     目录          0  2012-08-02 15:57  DelegateTest\obj\Debug\Refactor

     目录          0  2012-08-01 17:46  DelegateTest\obj\Debug\TempPE

     目录          0  2012-08-01 17:50  DelegateTest\bin\Debug

     目录          0  2012-08-02 15:57  DelegateTest\obj\Debug

     目录          0  2012-08-01 17:46  DelegateTest\bin

     目录          0  2012-08-01 17:46  DelegateTest\obj

     目录          0  2012-08-01 17:46  DelegateTest\Properties

     目录          0  2012-08-02 14:44  DelegateTest

----------- ---------  ---------- -----  ----

               129888                    28

............此处省略1个文件信息

评论

共有 条评论