2012.6.6文件操作,大文件传输
来源:互联网 发布:ubuntu编译安装php7.1 编辑:程序博客网 时间:2024/04/28 03:43
public Form1() { InitializeComponent(); label1.Text = ""; System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; } private void btnsel_Click(object sender, EventArgs e) { DialogResult dalogresult = openFileDialog1.ShowDialog(); if (DialogResult.OK == dalogresult) { txtpath.Text = openFileDialog1.FileName; } else { txtpath.Text = ""; } textBox2.Text = Path.Combine(textBox2.Text, Path.GetFileName(txtpath.Text)); } Thread th = null; private void btnstart_Click(object sender, EventArgs e) { label1.Text = ""; progressBar1.Visible = true; progressBar1.Minimum = 0; progressBar1.Maximum = 10000; progressBar1.Value = 0; th = new Thread(new ThreadStart(getth)); th.IsBackground = true; th.Start(); } double getprogressBarint(double sum,double num) { double x = sum / 10000; double barint = 0; barint = num / x; return barint; } void getth() { byte[] b = new byte[1024*1024]; using (FileStream sr = new FileStream(txtpath.Text, FileMode.Open)) { int i = 0; double sum = sr.Length; using (FileStream srr = new FileStream(textBox2.Text, FileMode.OpenOrCreate)) { while (sr.Read(b, i, b.Length) != 0) { srr.Write(b, i, b.Length); label1.Text ="已传输"+srr.Length+"个字节/共有" + sum+"字节"; double getint = getprogressBarint(sum, srr.Length); int print= Convert.ToInt16(getint); if (print >= progressBar1.Maximum) { progressBar1.Value = progressBar1.Maximum; } else { progressBar1.Value = print; } } } } progressBar1.Value = progressBar1.Maximum; th.Abort(); }