資源簡介
C#利用Remoting實現服務器與客戶端文件的上傳與下載
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?System.Runtime.Remoting;
using?System.IO;
using?System.Threading;
namespace?Client
{
public?partial?class?Form1?:?Form
{
public?Form1()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls?=?false;
????????????OpenUrl();
}
long?CountLen?=?0;
long?Len?=?0;
Remoteobject.Myobject?app;
public?void?Upload(string?filename)
{
if(filename==““)
{
MessageBox.Show(“請選擇要上傳的文件!““提示“);
return;
}
if(!File.Exists(filename))
{
MessageBox.Show(“上傳的文件不存在!““錯誤“);
return;
}
FileStream?fs?=?new?FileStream(filename?FileMode.Open);
CountLen=fs.Length;
long??Offset?=fs.Length/1000;
byte[]?byt?=?new?byte[1024*5];
Len?=?0;
while?(true)
{
int?templen?=?fs.Read(byt?0?byt.Length);
if?(templen?==?0)
{
MessageBox.Show(“文件寫入完成!“);
break;
}
else
{
Len?+=?templen;
int?tempvalue=(int)(Len?/?Offset);
if?(tempvalue?1000)
{
progressBar1.Value?=?tempvalue;
}
else
{
progressBar1.Value?=?1000;
}
button2.Text?=“上傳中...“+?((int)(((float)progressBar1.Value?/?(float)progressBar1.Maximum)?*?100)).ToString()?+?“%“;
label1.Text?=?“上傳:“?+?Convert.ToString(Len?/?1024)?+?“K?/?“?+?Convert.ToString(CountLen/1024)+?“K“;
if?(!app.Save(Path.GetFileName(filename)?byt))
{
MessageBox.Show(“文件寫入失敗!“);
}
}
}
fs.Close();
fs.Dispose();
}
private?void?button1_Click(object?sender?EventArgs?e)
{
if?(openFileDialog1.ShowDialog()?==?DialogResult.OK)
{
textBox1.Text?=?openFileDialog1.FileName;
}
}
Thread?th;
private?void?button2_Click(object?sender?EventArgs?e)
{
th?=?new?System.Threading.Thread(new?System.Threading.ThreadStart(DoWork));
th.Start();
}
public?void?DoWork()
{
button1.Enabled?=?false;
button2.Enabled?=?false;
button4.Enabled?=?false;
textBox2.ReadOnly=true;
app.DeleteFile(Path.GetFileName(textBox1.Text));
Upload(textBox1.Text);
button2.Text?=?“上傳“;
textBox2.ReadOnly=false;
button2.Enabled?=?true;
button1.Enabled?=?true;
button4.Enabled?=?true;
CountLen?=?0;
Len?=?0;
OldLen?=?0;
}
long?OldLen?=?0;
private?void?timer1_Tick(object?sender?EventArgs?e)
{
float?offs?=?(float)Len?/?(float)1024?-?(float)OldLen?/?(float)1024;
label2.Text?=?“當前速度?“?+?offs.ToString()?+?“?K/s“;
OldLen?=?Len;
if?((int)offs?!=?0)
{
label6.Text?=?Convert.ToString((CountLen?-?Len)?/?(long)offs)?+?“s“;
}
}
private?void?Form1_Load(object?sender?EventArgs?e)
{
timer1.Start();
button1.Enabled=false;
button2.Enabled=false;
button3.Enabled=false;
button4.Enabled=fals
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????28672??2009-07-29?16:37??Remoteob
?????文件??????36352??2009-07-29?16:37??Remoteob
?????文件???????5632??2005-12-08?14:51??Remoteob
?????文件??????16384??2009-07-29?16:33??Remoteob
?????文件??????13824??2009-07-29?16:33??Remoteob
?????文件???????3777??2009-07-29?16:25??Remoteob
?????文件???????6880??2009-07-29?16:37??Remoteob
?????文件??????13154??2009-07-29?16:36??Remoteob
?????文件???????6627??2009-07-29?16:36??Remoteob
?????文件????????329??2009-07-29?16:28??Remoteob
?????文件???????2312??2009-07-29?16:28??Remoteob
?????文件???????6551??2009-07-29?16:28??Remoteob
?????文件????????823??2009-07-29?16:40??Remoteob
?????文件????????899??2009-07-29?16:36??Remoteob
?????文件??????28672??2009-07-29?16:37??Remoteob
?????文件????????180??2009-07-29?16:36??Remoteob
?????文件????????879??2009-07-29?16:33??Remoteob
?????文件??????36352??2009-07-29?16:37??Remoteob
?????文件????????180??2009-07-29?16:33??Remoteob
?????文件???????2570??2009-07-29?16:33??Remoteob
?????文件????????465??2009-06-25?14:27??Remoteob
?????文件???????1160??2009-06-25?14:27??Remoteob
?????文件???????2868??2009-06-25?14:27??Remoteob
?????文件???????5612??2009-06-25?14:27??Remoteob
?????文件???????1091??2009-06-25?14:27??Remoteob
?????文件????????249??2009-06-25?14:27??Remoteob
?????文件??????16384??2009-07-29?16:33??Remoteob
?????文件??????13824??2009-07-29?16:33??Remoteob
?????文件???????4340??2009-07-16?11:44??Remoteob
?????文件??????16384??2009-07-29?16:33??Remoteob
............此處省略45個文件信息
- 上一篇:C#實現MQTT協議
- 下一篇:C#桌面右下角彈出提示窗
評論
共有 條評論