SoFunction
Updated on 2025-03-07

C# winform simulates ftp file management implementation code


using System;
using ;
using ;
using ;
using ;
using ;

 
namespace ConvertData
{
    class FtpUpDown
    {

        string ftpServerIP;
        string ftpUserID;
        string ftpPassword;
        FtpWebRequest reqFTP;

private void Connect(String path)//Connect ftp
        {
// Create FtpWebRequest object based on uri
            reqFTP = (FtpWebRequest)(new Uri(path));
// Specify the data transmission type
            = true;
// ftp username and password
            = new NetworkCredential(ftpUserID, ftpPassword);
        }

        public FtpUpDown(string ftpServerIP, string ftpUserID, string ftpPassword)
        {
            = ftpServerIP;
            = ftpUserID;
            = ftpPassword;
        }

//Everyone calls this
private string[] GetFileList(string path, string WRMethods)//The above code examples how to get a file list from an ftp server
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            try
            {
                Connect(path);
                = WRMethods;
                WebResponse response = ();
StreamReader reader = new StreamReader((), );//Chinese file name
                string line = ();
                while (line != null)
                {
                    (line);
                    ("\n");
                    line = ();
                }
                // to remove the trailing '\n'
                (().LastIndexOf('\n'), 1);
                ();
                ();
                return ().Split('\n');
            }
            catch (Exception ex)
            {
                ();
                downloadFiles = null;
                return downloadFiles;
            }
        }

public string[] GetFileList(string path)//The above code examples how to get a file list from the ftp server
        {
            return GetFileList("ftp://" + ftpServerIP + "/" + path, );

        }

 

public string[] GetFileList()//The above code examples how to get a file list from an ftp server
        {
            return GetFileList("ftp://" + ftpServerIP + "/", );
        }

public void Upload(string filename) //The above code implements the function of uploading files from the ftp server
        {
            FileInfo fileInf = new FileInfo(filename);
            string uri = "ftp://" + ftpServerIP + "/" + ;
Connect(uri);//Connect
// Default is true, the connection will not be closed
// Execute after a command
            = false;
// Specify what command to execute
            = ;
// Notify the server file size when uploading
            = ;
// Set the buffer size to kb
            int buffLength = 2048;

            byte[] buff = new byte[buffLength];
            int contentLen;
// Open a file stream() to read the uploaded file
            FileStream fs = ();
            try
            {
// Write the uploaded file to the stream
                Stream strm = ();
// KB of file stream is read every time
                contentLen = (buff, 0, buffLength);
// The streaming content has not ended
                while (contentLen != 0)
                {
// Write content from file stream to upload stream
                    (buff, 0, contentLen);
                    contentLen = (buff, 0, buffLength);
                }
// Close two streams
                ();
                ();
            }
            catch (Exception ex)
            {
                (, "Upload Error");
            }
        }
public bool Download(string filePath, string fileName, out string errorinfo)////The above code implements the function of downloading files from the ftp server
        {
            try
            {
                String onlyFileName = (fileName);
                string newFileName = filePath + "\\" + onlyFileName;
                if ((newFileName))
                {
errorinfo = ("The local file {0} already exists and cannot be downloaded", newFileName);
                    return false;
                }
                string url = "ftp://" + ftpServerIP + "/" + fileName;
Connect(url);//Connect
                = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)();
                Stream ftpStream = ();
                long cl = ;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                readCount = (buffer, 0, bufferSize);
                FileStream outputStream = new FileStream(newFileName, );

                while (readCount > 0)
                {
                    (buffer, 0, readCount);

                    readCount = (buffer, 0, bufferSize);
                }
                ();
                ();
                ();
                errorinfo = "";
                return true;
            }
            catch (Exception ex)
            {
errorinfo = ("Because {0}, cannot be downloaded", );
                return false;
            }
        }

//Delete the file

         public void DeleteFileName(string fileName)
         {
             try
             {
                 FileInfo fileInf = new FileInfo(fileName);
                 string uri = "ftp://" + ftpServerIP + "/" + ;
Connect(uri);//Connect
// Default is true, the connection will not be closed

// Execute after a command

                 = false;

// Specify what command to execute

                 = ;
                 FtpWebResponse response = (FtpWebResponse)();
                 ();
             }
             catch (Exception ex)
             {
(, "Delete error");
             }
         }

//Create a directory

        public void MakeDir(string dirName)
        {
            try
            {
                string uri = "ftp://" + ftpServerIP + "/" + dirName;
Connect(uri);//Connect
                = ;
                FtpWebResponse response = (FtpWebResponse)();
                ();
            }
            catch (Exception ex)
            {
                ();
            }
        }
//Delete the directory
        public void delDir(string dirName)
        {
            try
            {

                string uri = "ftp://" + ftpServerIP + "/" + dirName;
Connect(uri);//Connect
                = ;
                FtpWebResponse response = (FtpWebResponse)();
                ();
            }
            catch (Exception ex)
            {
                ();
            }
        }

//Get file size

        public long GetFileSize(string filename)
        {
            long fileSize = 0;
            try
            {
                FileInfo fileInf = new FileInfo(filename);
                string uri = "ftp://" + ftpServerIP + "/" + ;
Connect(uri);//Connect
                = ;
                FtpWebResponse response = (FtpWebResponse)();
                fileSize = ;
                ();
            }
            catch (Exception ex)
            {
                ();
            }
            return fileSize;
        }

//Rename the file
        public void Rename(string currentFilename, string newFilename)
        {
            try
            {
                FileInfo fileInf = new FileInfo(currentFilename);
                string uri = "ftp://" + ftpServerIP + "/" + ;
Connect(uri);//Connect
                = ;
                = newFilename;
                FtpWebResponse response = (FtpWebResponse)();
                //Stream ftpStream = ();

                //();
                ();
            }
            catch (Exception ex)
            {
                ();
            }
        }

//Get clear documentation
        public string[] GetFilesDetailList()
        {
            return GetFileList("ftp://" + ftpServerIP + "/", );
        }
//Get clear documentation
        public string[] GetFilesDetailList(string path)
        {
            return GetFileList("ftp://" + ftpServerIP + "/" + path, );
        }

    }
}