问高人一个关于进度条的问题:
我有如下一段代码,就是通过FTP方式向一个机器传电影,现在加了一个进度条,但是进度条的值一直不能满,代码如下:
private void FtpUpFile()
{
DataRow[] drAry = dtMMS.Select("mms_id='" + cbMMSServer.SelectedValue + "'"); FileInfo fileInf = new FileInfo(strFileName);
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(drAry[0]["strFTPAddress"].ToString() + fileInf.Name));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(drAry[0]["strFTPAccount"].ToString(), drAry[0]["strFTPPassword"].ToString());
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
// 缓冲大小设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead(); this.progressBar.Value = 0;
this.lalSuccess.Visible = false;
if ((int)(fs.Length / 1024) == 0)
{
//_progSend.Maximum = 1;
//_progSend.Value = 1;
this.progressBar.Maximum = (int)(fs.Length / 1024);
//this.progressBar.Maximum = 1;
this.progressBar.Value = 1;
}
else
//_progSend.Maximum = (int)(read.Length / 1024);
this.progressBar.Maximum = (int)(fs.Length / 1024);
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength);
int allbye = (int)fileInf.Length;
int startbye = 0;
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
startbye += contentLen;
if (this.progressBar.Value != this.progressBar.Maximum)
{
this.progressBar.Value ++;
} }
// 关闭两个流
strm.Close();
fs.Close();
this.lalSuccess.Visible = true;
m_upload = true;
}
catch (Exception ex)
{
m_upload = false;
}
}
我有如下一段代码,就是通过FTP方式向一个机器传电影,现在加了一个进度条,但是进度条的值一直不能满,代码如下:
private void FtpUpFile()
{
DataRow[] drAry = dtMMS.Select("mms_id='" + cbMMSServer.SelectedValue + "'"); FileInfo fileInf = new FileInfo(strFileName);
FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(drAry[0]["strFTPAddress"].ToString() + fileInf.Name));
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(drAry[0]["strFTPAccount"].ToString(), drAry[0]["strFTPPassword"].ToString());
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// 指定数据传输类型
reqFTP.UseBinary = true;
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
// 缓冲大小设置为2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead(); this.progressBar.Value = 0;
this.lalSuccess.Visible = false;
if ((int)(fs.Length / 1024) == 0)
{
//_progSend.Maximum = 1;
//_progSend.Value = 1;
this.progressBar.Maximum = (int)(fs.Length / 1024);
//this.progressBar.Maximum = 1;
this.progressBar.Value = 1;
}
else
//_progSend.Maximum = (int)(read.Length / 1024);
this.progressBar.Maximum = (int)(fs.Length / 1024);
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream();
// 每次读文件流的2kb
contentLen = fs.Read(buff, 0, buffLength);
int allbye = (int)fileInf.Length;
int startbye = 0;
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
startbye += contentLen;
if (this.progressBar.Value != this.progressBar.Maximum)
{
this.progressBar.Value ++;
} }
// 关闭两个流
strm.Close();
fs.Close();
this.lalSuccess.Visible = true;
m_upload = true;
}
catch (Exception ex)
{
m_upload = false;
}
}
解决方案 »
- C#调用ocx控件出错
- 微软企业库 5 ,设置缓存
- winform程序发布的问题。怎么解决.net framework?
- 发现今天回答问题的人多,高分给个难题
- C#禁止鼠标移动到界面上datagridview控件内!!在线等待!!!
- 选项卡式界面的问题
- 谁能给个管理MDSE用的orca的下载地址?
- 请高手介绍一本适合于初学者的C#的书
- 水晶报表:ReportDocument是控件吗?怎么我的.net Studio2003中没有?
- ComBox中如何列出从数据库中取出的数据???
- VS2005工具使用的简单问题,在线等。
- 在c#里如何用代碼在datagridview里某列變為checkbox ?
{
// 把内容从file stream 写入 upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
startbye += contentLen;
this.progressBar.Value = 100*startbye/allbye;
}