程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#文件斷點續傳完成辦法

C#文件斷點續傳完成辦法

編輯:C#入門知識

C#文件斷點續傳完成辦法。本站提示廣大學習愛好者:(C#文件斷點續傳完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#文件斷點續傳完成辦法正文


本文實例講述了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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved