C# winform 不知道最大值的进度条

大潘_IT 2011-03-01 06:12:37
我要拷贝一个文件夹覆盖另一个文件夹,在这个过程中我需要这样的进度条,请教高手!
...全文
297 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanzhehanzhe 2011-03-02
  • 打赏
  • 举报
回复
神奇的QQ空间
纯唇Yu弄 2011-03-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 dongxinxi 的回复:]

拷贝文件夹可以遍历统计出大小
C# code

public long GetDirectoryLength(string dirPath){
if(!Directory.Exists(dirPath))
return 0;
long len=0;
DirectoryInfo di=new DirectoryInfo(dirPath);
foreach(……
[/Quote]
火柴没帽 2011-03-02
  • 打赏
  • 举报
回复
判断文件夹内文件总数,根据拷贝完成的文件数量确定进度吧,不可能太精确
  • 打赏
  • 举报
回复
拷贝文件夹可以遍历统计出大小

public long GetDirectoryLength(string dirPath){
if(!Directory.Exists(dirPath))
return 0;
long len=0;
DirectoryInfo di=new DirectoryInfo(dirPath);
foreach(FileInfo fi in di.GetFiles()){
len+=fi.Length;
}
DirectoryInfo[] dis=di.GetDirectories();
if(dis.Length> 0){
for(int i=0;i <dis.Length;i++){
len+=GetDirectoryLength(dis[i].FullName);
}
}
return len;
}

  • 打赏
  • 举报
回复

private void initializecomponent()
{
//
// progressbar1
//
this.progressbar1.location = new system.drawing.point(8, 16);
this.progressbar1.name = "progressbar1";
this.progressbar1.size = new system.drawing.size(208, 16);
this.progressbar1.tabindex = 0;
...
}

int totalsize; //total size
int position; //position
const int buffer_size = 4096;
byte[] buffer;
stream stream;

private void btncopy_click(object sender, system.eventargs e)
{
string strfile = "";

openfiledialog dlg = new openfiledialog();
if ( dlg.showdialog() == dialogresult.ok )
{
strfile = dlg.filename ;
}
else
{
return ;
}

filestream fs = new filestream( strfile , filemode.open , fileaccess.read ) ;
totalsize = (int)fs.length ;
stream = fs;

//delete file which aready exists.
if ( file.exists( "c:\\copyedfile.bin" ) )
file.delete( "c:\\copyedfile.bin" );

//copy file while larger than 4kb.
if ( totalsize > buffer_size )
{
buffer = new byte[ buffer_size ];

// async invoke
stream.beginread( buffer , 0 , buffer_size , new asynccallback( asynccopyfile ) , null );
}
else
{
fs.close();
}

}

/// <summary>
/// asynchronously copy file
/// </summary>
/// <param name="ar"></param>
private void asynccopyfile( iasyncresult ar )
{
int readedlength ;

// lock filestream
lock( stream )
{
readedlength = stream.endread( ar ); // when stream endread, get readed length
}

// write to disk
filestream fswriter = new filestream( "c:\\copyedfile.bin" , filemode.append , fileaccess.write );
fswriter.write( buffer , 0 , buffer.length );
fswriter.close();

// current stream position
position += readedlength;

// response ui
methodinvoker m = new methodinvoker( synchprogressbar );
m.begininvoke( null , null );

if ( position >= totalsize ) // read over.
{
stream.close(); //close filestream
return ;
}

// continue to read and write
lock ( stream )
{
int leftsize = totalsize - position;

if ( leftsize < buffer_size )
buffer = new byte[ leftsize ];

stream.beginread( buffer , 0 , buffer.length , new asynccallback( asynccopyfile ) , null );

}
}

private void synchprogressbar()
{
this.progressbar1.maximum = totalsize;
this.progressbar1.value = position ;
}
Dobzhansky 2011-03-01
  • 打赏
  • 举报
回复
这是个进度条?

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧