欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#文件断点续传实现方法

程序员文章站 2023-11-28 09:07:22
本文实例讲述了c#文件断点续传实现方法。分享给大家供大家参考。具体实现方法如下: /// /// 下载局域网文件 /// &l...

本文实例讲述了c#文件断点续传实现方法。分享给大家供大家参考。具体实现方法如下:

/// <summary>
/// 下载局域网文件
/// </summary>
/// <param name="path">文件路径,如:\\192.168.10.1\app\app\123.zip</param>
/// <param name="username">计算机名称</param>
/// <param name="password">计算机密码</param>
static void requestwindowsshared(string path, string username, string password)
{
 //文件总大小
 int allbytescount = 0;
 //每次传输大小
 int bytetemp = 1024;
 //当前位置
 int byteposition = 0;
 //剩下大小
 int remain = 0;
 system.net.filewebrequest request = null;
 system.net.filewebresponse response = null;
 system.io.stream stream = null;
 system.io.filestream filestream = null;
 try
 {
  uri uri = new uri(path);
  request = (system.net.filewebrequest)system.net.filewebrequest.create(uri);
  system.net.icredentials ic = new system.net.networkcredential(username, password);
  request.credentials = ic;
  response = (system.net.filewebresponse)request.getresponse();
  stream = response.getresponsestream();
  byte[] bytes = new byte[stream.length];
  stream.read(bytes, 0, bytes.length);
  string filename = system.environment.getfolderpath(environment.specialfolder.desktop) + "\\" + system.io.path.getfilename(path);
  filestream = new filestream(filename, system.io.filemode.create, system.io.fileaccess.write, system.io.fileshare.readwrite);
  allbytescount = bytes.length;
  remain = allbytescount;
  while (remain > 0)
  {
   filestream.write(bytes, byteposition, bytetemp);
   remain = remain - bytetemp;
   byteposition = byteposition + bytetemp;
   filestream.flush();
   if (remain < bytetemp)
    bytetemp = remain;
  }
  console.writeline("下载成功!");
 }
 catch (exception ex)
 {
  console.writeline(ex.message);
 }
 finally
 {
  filestream.close();
  filestream.dispose();
  stream.close();
  stream.dispose();
 }
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="path">共享目录路径+文件名称</param>
/// <param name="local">本地路径</param>
/// <param name="username">用户名</param>
/// <param name="password">密码</param>
static void responsewindowsshared(string path, string local, string username, string password)
{
 //文件总大小
 int allbytescount = 0;
 //每次传输大小
 int bytetemp = 1024;
 //当前位置
 int byteposition = 0;
 //剩下大小
 int remain = 0;
 system.net.filewebrequest request = null;
 system.io.stream stream = null;
 try
 {
  //时间戳 
  string strboundary = "----------" + datetime.now.ticks.tostring("x");
  uri uri = new uri(path);
  byte[] bytes = system.io.file.readallbytes(local);
  request = (system.net.filewebrequest)system.net.filewebrequest.create(uri);
  request.method = "post";
  //设置获得响应的超时时间(300秒) 
  request.timeout = 300000;
  request.contenttype = "multipart/form-data; boundary=" + strboundary;
  request.contentlength = bytes.length;
  system.net.icredentials ic = new system.net.networkcredential(username, password);
  request.credentials = ic;
  stream = request.getrequeststream();
  allbytescount = bytes.length;
  remain = allbytescount;
  while (remain > 0)
  {
   stream.write(bytes, byteposition, bytetemp);
   remain = remain - bytetemp;
   byteposition = byteposition + bytetemp;
   stream.flush();
   if (remain < bytetemp)
    bytetemp = remain;
  }
  console.writeline("上传成功!");
 }
 catch (exception ex)
 {
  console.writeline(ex.message);
 }
 finally
 {
  stream.close();
  stream.dispose();
 }
}

希望本文所述对大家的c#程序设计有所帮助。