SoFunction
Updated on 2025-03-08

Example of C# implementing FTP file transfer

Introduction:

Continuing the above implementation of the FTP transmission file, this article is exactly the same as the above, but this article is used to establish a connection through cmd, and the following steps are also implemented after establishing the connection. The hierarchical structure of a file is as mentioned above, so I won’t go into details here.C# implements FTP upload information

1. Call the main method:

(vIMSPath, vUID, vPassword, vLocalPath + "/" + txtFile, txtFile);

How to implement the operation in the file

2.1 Methods called in the main method:

public void UploadFile(string vPath, string vUID, string vPassword, string vLocalPath, string file)
    {
      bool status = false;
      //
      status = connectState(vPath, vUID, vPassword);//Create a connection through cmd      if (status)
      {
        DirectoryInfo theFolder = new DirectoryInfo(vPath + "/" + file);
        string filename = vLocalPath;
        Transport(vLocalPath, vPath + "/" + file);//Send files        (vPath);
      }
      else
      {
        ("Failed to connect!");
      }
    }

2.2 Make a connection by calling cmd:

public static bool connectState(string vPath, string vUID, string vPassword)
    {
      bool Flag = false;
      Process proc = new Process();
      try
      {
         = "";
         = false;
         = true;
         = true;
         = true;
         = true;
        ();
        string dosLine = "net use " + vPath + " " + vPassword + "/user:" + vUID;
        (dosLine);
        ("exit");
        while (!)
        {
          (1000);
        }
        string errormsg = ();
        ();
        if ((errormsg))
        {
          Flag = true;
        }
        else
        {
          throw new Exception(errormsg);
        }
      }
      catch (Exception ex)
      {
        //throw ex;
        ();
      }
      finally
      {
        ();
        ();
      }
      return Flag;
    }

2.3 Transfer files:

public static void Transport(string src, string fileName)
    {
      FileStream inFileStream = new FileStream(src, );
      //if (!(dst))
      //{
      //  (src,dst);
      //}
      FileStream outFileStream = new FileStream(fileName, );

      byte[] buf = new byte[];
      int byteCount;
      while ((byteCount = (buf, 0, )) > 0)
      {
        (buf, 0, byteCount);
      }
      ();
      ();
      ();
      ();
      (src);//Delete local files    }

The above is the detailed content of the example of C# implementing FTP file transfer. For more information about C# ftp file transfer, please pay attention to my other related articles!