SoFunction
Updated on 2025-03-08

Implementation method of C# file breakpoint continuous transmission

This article describes the implementation method of C# file breakpoint continuous transmission. Share it for your reference. The specific implementation method is as follows:

/// <summary>
/// Download LAN files/// </summary>
/// <param name="path">File path, such as: \\192.168.10.1\app\app\app\</param>/// <param name="username">Computer name</param>/// <param name="password">Computer Password</param>static void RequestWindowsShared(string path, string username, string password)
{
 //Total file size int allBytesCount = 0;
 //Size per transfer int byteTemp = 1024;
 //Current location int bytePosition = 0;
 //The remaining size int remain = 0;
  request = null;
  response = null;
  stream = null;
  fileStream = null;
 try
 {
  Uri uri = new Uri(path);
  request = ()(uri);
   ic = new (username, password);
   = ic;
  response = ()();
  stream = ();
  byte[] bytes = new byte[];
  (bytes, 0, );
  string filename = () + "\\" + (path);
  fileStream = new FileStream(filename, , , );
  allBytesCount = ;
  remain = allBytesCount;
  while (remain &gt; 0)
  {
   (bytes, bytePosition, byteTemp);
   remain = remain - byteTemp;
   bytePosition = bytePosition + byteTemp;
   ();
   if (remain &lt; byteTemp)
    byteTemp = remain;
  }
  ("Download successfully!");
 }
 catch (Exception ex)
 {
  ();
 }
 finally
 {
  ();
  ();
  ();
  ();
 }
}
/// &lt;summary&gt;
/// Upload file/// &lt;/summary&gt;
/// <param name="path">Shared directory path + file name</param>/// <param name="local">local path</param>/// <param name="username">username</param>/// <param name="password">Password</param>static void ResponseWindowsShared(string path, string local, string username, string password)
{
 //Total file size int allBytesCount = 0;
 //Size per transfer int byteTemp = 1024;
 //Current location int bytePosition = 0;
 //The remaining size int remain = 0;
  request = null;
  stream = null;
 try
 {
  //Time stamp  string strBoundary = "----------" + ("x");
  Uri uri = new Uri(path);
  byte[] bytes = (local);
  request = ()(uri);
   = "POST";
  //Set the timeout time to get the response (300 seconds)   = 300000;
   = "multipart/form-data; boundary=" + strBoundary;
   = ;
   ic = new (username, password);
   = ic;
  stream = ();
  allBytesCount = ;
  remain = allBytesCount;
  while (remain &gt; 0)
  {
   (bytes, bytePosition, byteTemp);
   remain = remain - byteTemp;
   bytePosition = bytePosition + byteTemp;
   ();
   if (remain &lt; byteTemp)
    byteTemp = remain;
  }
  ("Uploaded successfully!");
 }
 catch (Exception ex)
 {
  ();
 }
 finally
 {
  ();
  ();
 }
}

I hope this article will be helpful to everyone's C# programming.