c#

位置:IT落伍者 >> c# >> 浏览文章

C# FTP操作类


发布日期:2018年12月04日
 
C# FTP操作类

最近要做个用ftp读取文件并在浏览器中直接打开图片的小程序最终写了这个FTP类库发上来给大家共享共享

using System;

using SystemCollectionsGeneric;

using SystemText;

using SystemNet;

using SystemIO;

namespace Utility

{

public class FtpUpDown

{

string ftpServerIP;

string ftpUserID;

string ftpPassword;

FtpWebRequest reqFTP;

private void Connect(String path)//连接ftp

{

// 根据uri创建FtpWebRequest对象

reqFTP = (FtpWebRequest)FtpWebRequestCreate(new Uri(path));

// 指定数据传输类型

reqFTPUseBinary = true;

// ftp用户名和密码

reqFTPCredentials = new NetworkCredential(ftpUserID ftpPassword);

}

public FtpUpDown(string ftpServerIP string ftpUserID string ftpPassword)

{

thisftpServerIP = ftpServerIP;

thisftpUserID = ftpUserID;

thisftpPassword = ftpPassword;

}

//都调用这个

private string[] GetFileList(string path string WRMethods)//上面的代码示例了如何从ftp服务器上获得文件列表

{

string[] downloadFiles;

StringBuilder result = new StringBuilder();

try

{

Connect(path);

reqFTPMethod = WRMethods;

WebResponse response = reqFTPGetResponse();

StreamReader reader = new StreamReader(responseGetResponseStream() SystemTextEncodingDefault);//中文文件名

string line = readerReadLine();

while (line != null)

{

resultAppend(line);

resultAppend(\n);

line = readerReadLine();

}

// to remove the trailing \n

resultRemove(resultToString()LastIndexOf(\n) );

readerClose();

responseClose();

return resultToString()Split(\n);

}

catch (Exception ex)

{

LogWriteError(Get FileList Error: + exMessage);

downloadFiles = null;

return downloadFiles;

}

}

public string[] GetFileList(string path)//上面的代码示例了如何从ftp服务器上获得文件列表

{

return GetFileList(ftp:// + ftpServerIP + / + path WebRequestMethodsFtpListDirectory);

}

public string[] GetFileList()//上面的代码示例了如何从ftp服务器上获得文件列表

{

return GetFileList(ftp:// + ftpServerIP + / WebRequestMethodsFtpListDirectory);

}

public void Upload(string filename) //上面的代码实现了从ftp服务器上载文件的功能

{

FileInfo fileInf = new FileInfo(filename);

string uri = ftp:// + ftpServerIP + / + fileInfName;

Connect(uri);//连接

// 默认为true连接不会被关闭

// 在一个命令之后被执行

reqFTPKeepAlive = false;

// 指定执行什么命令

reqFTPMethod = WebRequestMethodsFtpUploadFile;

// 上传文件时通知服务器文件的大小

reqFTPContentLength = fileInfLength;

// 缓沖大小设置为kb

int buffLength = ;

byte[] buff = new byte[buffLength];

int contentLen;

// 打开一个文件流(SystemIOFileStream) 去读上传的文件

FileStream fs = fileInfOpenRead();

try

{

// 把上传的文件写入流

Stream strm = reqFTPGetRequestStream();

// 每次读文件流的kb

contentLen = fsRead(buff buffLength);

// 流内容没有结束

while (contentLen != )

{

// 把内容从file stream 写入upload stream

strmWrite(buff contentLen);

contentLen = fsRead(buff buffLength);

}

// 关闭两个流

strmClose();

fsClose();

}

catch (Exception ex)

{

LogWriteError( Upload Error: + exMessage);

}

}

public bool Download(string filePath string fileName out string errorinfo)////上面的代码实现了从ftp服务器下载文件的功能

{

try

{

String onlyFileName = PathGetFileName(fileName);

string newFileName = filePath + \\ + onlyFileName;

if (FileExists(newFileName))

{

errorinfo = stringFormat(本地文件{}已存在无法下载 newFileName);

return false;

}

string url = ftp:// + ftpServerIP + / + fileName;

Connect(url);//连接

reqFTPCredentials = new NetworkCredential(ftpUserID ftpPassword);

FtpWebResponse response = (FtpWebResponse)reqFTPGetResponse();

Stream ftpStream = responseGetResponseStream();

long cl = responseContentLength;

int bufferSize = ;

int readCount;

byte[] buffer = new byte[bufferSize];

readCount = ftpStreamRead(buffer bufferSize);

FileStream outputStream = new FileStream(newFileName FileModeCreate);

while (readCount > )

{

outputStreamWrite(buffer readCount);

readCount = ftpStreamRead(buffer bufferSize);

}

ftpStreamClose();

outputStreamClose();

responseClose();

errorinfo = ;

return true;

}

catch (Exception ex)

{

errorinfo = stringFormat(因{}无法下载 exMessage);

return false;

}

}

//删除文件

public void DeleteFileName(string fileName)

{

try

{

FileInfo fileInf = new FileInfo(fileName);

string uri = ftp:// + ftpServerIP + / + fileInfName;

Connect(uri);//连接

// 默认为true连接不会被关闭

// 在一个命令之后被执行

reqFTPKeepAlive = false;

// 指定执行什么命令

reqFTPMethod = WebRequestMethodsFtpDeleteFile;

FtpWebResponse response = (FtpWebResponse)reqFTPGetResponse();

responseClose();

}

catch (Exception ex)

{

LogWriteError(删除错误: + exMessage);

}

}

//创建目录

public void MakeDir(string dirName)

{

try

{

string uri = ftp:// + ftpServerIP + / + dirName;

Connect(uri);//连接

reqFTPMethod = WebRequestMethodsFtpMakeDirectory;

FtpWebResponse response = (FtpWebResponse)reqFTPGetResponse();

responseClose();

}

catch (Exception ex)

{

LogWriteError(创建目录错误: + exMessage);

}

}

//删除目录

public void delDir(string dirName)

{

try

{

string uri = ftp:// + ftpServerIP + / + dirName;

Connect(uri);//连接

reqFTPMethod = WebRequestMethodsFtpRemoveDirectory;

FtpWebResponse response = (FtpWebResponse)reqFTPGetResponse();

responseClose();

}

catch (Exception ex)

{

LogWriteError(删除目录错误: + exMessage);

}

}

//获得文件大小

public long GetFileSize(string filename)

{

long fileSize = ;

try

{

FileInfo fileInf = new FileInfo(filename);

string uri = ftp:// + ftpServerIP + / + fileInfName;

Connect(uri);//连接

reqFTPMethod = WebRequestMethodsFtpGetFileSize;

FtpWebResponse response = (FtpWebResponse)reqFTPGetResponse();

fileSize = responseContentLength;

responseClose();

}

catch (Exception ex)

{

LogWriteError(获得文件大小错误: + exMessage);

}

return fileSize;

}

//文件改名

public void Rename(string currentFilename string newFilename)

{

try

{

FileInfo fileInf = new FileInfo(currentFilename);

string uri = ftp:// + ftpServerIP + / + fileInfName;

Connect(uri);//连接

reqFTPMethod = WebRequestMethodsFtpRename;

reqFTPRenameTo = newFilename;

FtpWebResponse response = (FtpWebResponse)reqFTPGetResponse();

responseClose();

}

catch (Exception ex)

{

LogWriteError(文件改名错误: + exMessage);

}

}

//读取文件

public Stream ReadFile(string fileName)

{

try

{

string url = ftp:// + ftpServerIP + / + fileName;

Connect(url);//连接

reqFTPCredentials = new NetworkCredential(ftpUserID ftpPassword);

FtpWebResponse response = (FtpWebResponse)reqFTPGetResponse();

Stream ftpStream = responseGetResponseStream();

return ftpStream;

}

catch (Exception ex)

{

LogWriteError(读取文件错误: + exMessage);

return null;

}

}

//获得文件明晰

public string[] GetFilesDetailList()

{

return GetFileList(ftp:// + ftpServerIP + / WebRequestMethodsFtpListDirectoryDetails);

}

//获得文件明晰

public string[] GetFilesDetailList(string path)

{

return GetFileList(ftp:// + ftpServerIP + / + path WebRequestMethodsFtpListDirectoryDetails);

}

// 文件存在检查

public bool fileCheckExist(string fileName)

{

bool success = false;

FtpWebResponse response = null;

StreamReader reader = null;

try

{

string url = ftp:// + ftpServerIP + / + fileName;

Connect(url);//连接

reqFTPCredentials = new NetworkCredential(ftpUserID ftpPassword);

response = (FtpWebResponse)reqFTPGetResponse();

reader = new StreamReader(responseGetResponseStream());

string line = readerReadLine();

if (line != null)

{

success = true;

}

}

catch (Exception)

{

success = false;

}

finally

{

if (reader != null)

{

readerClose();

}

if (response != null)

{

responseClose();

}

}

return success;

}

}

}

               

上一篇:Microsoft .NET 框架资源基础

下一篇:C#中abstract关键字的使用方法