c#FTP基本使用
Posted huangxuqaq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); } } }
以上是关于c#FTP基本使用的主要内容,如果未能解决你的问题,请参考以下文章
html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。
为啥我的 C 代码片段不起作用?简化版可以。为 unsigned long long 传递不带 VA_ARGS 的 args