c#FTP基本使用

点我看看

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'), 1);
                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 -1;
            }
        }

        //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 = 5120;
                                byte[] buffer = new byte[bufferLength];
                                int i;
                                while ((i = fs.Read(buffer, 0, bufferLength)) > 0)
                                {
                                    stream.Write(buffer, 0, 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 = 5120;
                int i;
                byte[] buffer = new byte[bufferLength];
                i = stream.Read(buffer, 0, bufferLength);
                while (i > 0)
                {
                    fs.Write(buffer, 0, i);
                    i = stream.Read(buffer, 0, 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);
            }
        }
    }
posted @ 2021-03-12 17:27  vvull  阅读(331)  评论(0编辑  收藏  举报