c#FTP基本使用
阅读原文时间:2023年07月13日阅读:1

public class FtpHelper
{
//基本设置
private static string ftppath = @"ftp://" + "192.168.1.1" + "/";
private static string username = "username";
private static string password = "password";

    //获取FTP上面的文件夹和文件  
    public static string\[\] GetFolderAndFileList(string s)  
    {  
        string\[\] getfolderandfilelist;  
        FtpWebRequest request;  
        StringBuilder sb = new StringBuilder();  
        try  
        {  
            request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath));  
            request.UseBinary = true;  
            request.Credentials = new NetworkCredential(username, password);  
            request.Method = WebRequestMethods.Ftp.ListDirectory;  
            request.UseBinary = true;  
            WebResponse response = request.GetResponse();  
            StreamReader reader = new StreamReader(response.GetResponseStream());  
            string line = reader.ReadLine();  
            while (line != null)  
            {  
                sb.Append(line);  
                sb.Append("\\n");  
                Console.WriteLine(line);  
                line = reader.ReadLine();  
            }  
            sb.Remove(sb.ToString().LastIndexOf('\\n'), );  
            reader.Close();  
            response.Close();  
            return sb.ToString().Split('\\n');  
        }  
        catch (Exception ex)  
        {  
            Console.WriteLine("获取FTP上面的文件夹和文件:" + ex.Message);  
            getfolderandfilelist = null;  
            return getfolderandfilelist;  
        }  
    }

    //获取FTP上面的文件大小  
    public static int GetFileSize(string fileName)  
    {  
        FtpWebRequest request;  
        try  
        {  
            request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath + fileName));  
            request.UseBinary = true;  
            request.Credentials = new NetworkCredential(username, password);  
            request.Method = WebRequestMethods.Ftp.GetFileSize;  
            int n = (int)request.GetResponse().ContentLength;  
            return n;  
        }  
        catch (Exception ex)  
        {  
            Console.WriteLine("获取FTP上面的文件大小:" + ex.Message);  
            return -;  
        }  
    }

    //FTP上传文件  
    public static void FileUpLoad(string filePath, string objPath = "")  
    {  
        try  
        {  
            string url = ftppath;  
            if (objPath != "")  
                url += objPath + "/";  
            try  
            {  
                FtpWebRequest request = null;  
                try  
                {  
                    FileInfo fi = new FileInfo(filePath);  
                    using (FileStream fs = fi.OpenRead())  
                    {  
                        request = (FtpWebRequest)FtpWebRequest.Create(new Uri(url + fi.Name));  
                        request.Credentials = new NetworkCredential(username, password);  
                        request.KeepAlive = false;  
                        request.Method = WebRequestMethods.Ftp.UploadFile;  
                        request.UseBinary = true;  
                        using (Stream stream = request.GetRequestStream())  
                        {  
                            int bufferLength = ;  
                            byte\[\] buffer = new byte\[bufferLength\];  
                            int i;  
                            while ((i = fs.Read(buffer, , bufferLength)) > )  
                            {  
                                stream.Write(buffer, , i);  
                            }  
                            Console.WriteLine("FTP上传文件succesful");  
                        }  
                    }  
                }  
                catch (Exception ex)  
                {  
                    Console.WriteLine("FTP上传文件:" + ex.Message);  
                }  
                finally  
                {

                }  
            }  
            catch (Exception ex)  
            {  
                Console.WriteLine("FTP上传文件:" + ex.Message);  
            }  
            finally  
            {

            }  
        }  
        catch (Exception ex)  
        {  
            Console.WriteLine("FTP上传文件:" + ex.Message);  
        }  
    }

    //FTP下载文件  
    public static void FileDownLoad(string fileName)  
    {  
        FtpWebRequest request;  
        try  
        {  
            string downloadPath = @"D:";  
            FileStream fs = new FileStream(downloadPath + "\\\\" + fileName, FileMode.Create);  
            request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath + fileName));  
            request.Method = WebRequestMethods.Ftp.DownloadFile;  
            request.UseBinary = true;  
            request.Credentials = new NetworkCredential(username, password);  
            request.UsePassive = false;  
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();  
            Stream stream = response.GetResponseStream();  
            int bufferLength = ;  
            int i;  
            byte\[\] buffer = new byte\[bufferLength\];  
            i = stream.Read(buffer, , bufferLength);  
            while (i > )  
            {  
                fs.Write(buffer, , i);  
                i = stream.Read(buffer, , bufferLength);  
            }  
            stream.Close();  
            fs.Close();  
            response.Close();  
        }  
        catch (Exception ex)  
        {  
            Console.WriteLine("FTP下载文件:" + ex.Message);  
        }  
    }

    //FTP删除文件  
    public static void FileDelete(string fileName)  
    {  
        try  
        {  
            string uri = ftppath + fileName;  
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
            request.UseBinary = true;  
            request.Credentials = new NetworkCredential(username, password);  
            request.KeepAlive = false;  
            request.Method = WebRequestMethods.Ftp.DeleteFile;  
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();  
            response.Close();  
        }  
        catch (Exception ex)  
        {  
            Console.WriteLine("FTP删除文件:" + ex.Message);  
        }  
    }

    //FTP新建目录,上一级须先存在  
    public static void MakeDir(string dirName)  
    {  
        try  
        {  
            string uri = ftppath + dirName;  
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
            request.UseBinary = true;  
            request.Credentials = new NetworkCredential(username, password);  
            request.Method = WebRequestMethods.Ftp.MakeDirectory;  
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();  
            response.Close();  
        }  
        catch (Exception ex)  
        {  
            Console.WriteLine("FTP新建目录:" + ex.Message);  
        }  
    }

    //FTP删除目录,上一级须先存在  
    public static void DelDir(string dirName)  
    {  
        try  
        {  
            string uri = ftppath + dirName;  
            FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));  
            reqFTP.Credentials = new NetworkCredential(username, password);  
            reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;  
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
            response.Close();  
        }  
        catch (Exception ex)  
        {  
            Console.WriteLine("FTP删除目录:" + ex.Message);  
        }  
    }  
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章