如何在使用C#.NET Windows应用程序从ftp下载文件期间显示kbps和总文件大小? [英] How to display kbps and total file size during file download from ftp using C# .NET windows application?

查看:68
本文介绍了如何在使用C#.NET Windows应用程序从ftp下载文件期间显示kbps和总文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



如何在文件下载期间在标签上显示kbps和总文件大小来自 ftp 使用 c#.net windows应用程序?





但是收到错误: -

跨线程操作无效:控制'  label1'从一个线程访问,而不是创建它的线程。




此行中的
: -

 labelProgress.Text = Convert.ToString(iProgress); 





请帮帮我。



先谢谢。



Ankit Agarwal

软件工程师



我的尝试:



string [] files = ReadFileList();

FTPSettings.IP =xxxx;

FTPSettings.UserID =xxxxx;

FTPSettings.Password =xxxxx;

// FtpWebRequest reqFTP = null;

//流ftpStream = null ;

foreach(文件中的字符串文件)

{

//字符串fileName = e.Argument.ToString();



FtpWebRequest request =(FtpWebRequest)WebRequest.Create(ftp://+ FTPSettings.IP +/+ file);

请求。凭证=新的NetworkCredential(FTPSettings.UserID,FTPSettings.Password);

request.Method = WebRequestMethods.Ftp.GetFileSize;

request.Proxy = null;



long fileSize; //这是ReportProgress的关键

使用(WebResponse resp = request.GetResponse())

fileSize = resp.ContentLength;



request =(FtpWebRequest)WebRequest.Create(ftp://+ FTPSettings.IP +/+ file);

request.Credentials = new NetworkCredential(FTPSettings .UserID,FTPSettings.Password);

request.Method = WebRequestMethods.Ftp.DownloadFile;

using(FtpWebResponse responseFileDownload =(FtpWebResponse)request.GetResponse())

using(Stream responseStream = responseFileDownload.GetResponseStream())

using(FileStream writeStream = new FileStream(@C:\abc\+ file,FileMode.Create) )

{



int Length = 2048;

Byte [] buffer = new Byte [Length];

int bytesRead = responseStream.Read(buffer,0,Length);

int bytes = 0;



while(bytesRead> 0)

{

writeStream.Write(buffer,0,bytesRead);

bytesRead = responseStream.Read(buffer,0,Length) ;

bytes + = bytesRead; //不要忘记增加bytesRead!

int iProgress = 0;

int totalSize =(int )(fileSize)/ 1000; // Kbytes

if(totalSize> 0)

{

iProgress =(bytes / 1000)* 100 / totalSize;

}

labelProgress.Text = Convert.ToString(iProgress);

labelTotalSize.Text = Convert.ToString(totalSize);

backgroundWorker1.ReportProgress(iProgress,totalSize);



}

}

解决方案

查看错误消息:

跨线程操作无效:控制'label1'从其创建的线程以外的线程访问。 



非常明确。

你不能从任何线程访问任何UI控件,而不是它所创建的线程 - UI线程。

您的代码在不同的线程上运行 - 可能是由于FTP操作完成事件或类似 - 所以访问控件你必须调用它:如何:操纵控件来自主题 [ ^ ]


Hello,

How to display kbps and total file size on label during file download from ftp using c# .net windows application?



but its getting error:-

Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.



in this line:-

labelProgress.Text = Convert.ToString(iProgress);



Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

string[] files = ReadFileList();
FTPSettings.IP = "xxxx";
FTPSettings.UserID = "xxxxx";
FTPSettings.Password = "xxxxx";
//FtpWebRequest reqFTP = null;
//Stream ftpStream = null;
foreach (string file in files)
{
//string fileName = e.Argument.ToString();

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + FTPSettings.IP + "/" + file);
request.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Proxy = null;

long fileSize; // this is the key for ReportProgress
using (WebResponse resp = request.GetResponse())
fileSize = resp.ContentLength;

request = (FtpWebRequest)WebRequest.Create("ftp://" + FTPSettings.IP + "/" + file);
request.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (FtpWebResponse responseFileDownload = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = responseFileDownload.GetResponseStream())
using (FileStream writeStream = new FileStream(@"C:\abc\" + file, FileMode.Create))
{

int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
int bytes = 0;

while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
bytes += bytesRead;// don't forget to increment bytesRead !
int iProgress = 0;
int totalSize = (int)(fileSize) / 1000; // Kbytes
if (totalSize > 0)
{
iProgress = (bytes / 1000) * 100 / totalSize;
}
labelProgress.Text = Convert.ToString(iProgress);
labelTotalSize.Text = Convert.ToString(totalSize);
backgroundWorker1.ReportProgress(iProgress, totalSize);

}
}

解决方案

Look at the error message:

Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.


It's pretty explicit.
You cannot access any UI control from any thread other than the thread it was cretaed on - the UI thread.
Your code is running on a different thread - probably as a result of a "FTP operation completed" event or similar - so to access the control you would have to Invoke it: How to: Manipulate Controls from Threads[^]


这篇关于如何在使用C#.NET Windows应用程序从ftp下载文件期间显示kbps和总文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆