志在指尖
用双手敲打未来

FTPHelper,FTP公共帮助类同享

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Net;
usingSystem.IO;
usingSystem.Threading;
usingSystem.Configuration;
namespaceFtpSyn
{
publicclassFtpHelper
{
//根本设置[url]ftp://400:ZOina2017@192.168.10.17/400backup[/url]
staticprivatestringpath=@”ftp://”+ConfigurationManager.AppSettings[“FtpServerIP”].ToString()+”/”;//方针途径
staticprivatestringftpip=ConfigurationManager.AppSettings[“FtpServerIP”].ToString();//ftpIP地址
staticprivatestringusername=ConfigurationManager.AppSettings[“FtpUserName”].ToString();//ftp用户名
staticprivatestringpassword=ConfigurationManager.AppSettings[“FtpPassWord”].ToString();//ftp暗码
//获取ftp上面的文件和文件夹c#
publicstaticstring[]GetFileList(stringdir)
{
string[]downloadFiles;
StringBuilderresult=newStringBuilder();
FtpWebRequestrequest;
try
{
request=(FtpWebRequest)FtpWebRequest.Create(newUri(path+dir));
request.UseBinary=true;
request.Credentials=newNetworkCredential(username,password);//设置用户名和暗码
request.Method=WebRequestMethods.Ftp.ListDirectory;
request.UseBinary=true;
request.UsePassive=false;//挑选自动仍是被动模式,这句要加上的。
request.KeepAlive=false;//一定要设置此特点,不然一次性下载多个文件的时分,会出现异常。
WebResponseresponse=request.GetResponse();
StreamReaderreader=newStreamReader(response.GetResponseStream());
stringline=reader.ReadLine();
while(line!=null)
{
result.Append(line);
result.Append(“\n”);
line=reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf(‘\n’),1);
reader.Close();
response.Close();
returnresult.ToString().Split(‘\n’);
}
catch(Exceptionex)
{
LogHelper.writeErrorLog(“获取ftp上面的文件和文件夹:”+ex.Message);
downloadFiles=null;
returndownloadFiles;
}
}
///
///从ftp服务器上获取文件并将内容悉数转换成string返回
///
///
///
///
publicstaticstringGetFileStr(stringfileName,stringdir)
{
FtpWebRequestreqFTP;
try
{
reqFTP=(FtpWebRequest)FtpWebRequest.Create(newUri(path+dir+”/”+fileName));
reqFTP.Method=WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary=true;
reqFTP.Credentials=newNetworkCredential(username,password);
reqFTP.UsePassive=false;//挑选自动仍是被动模式,这句要加上的。
reqFTP.KeepAlive=false;//一定要设置此特点,不然一次性下载多个文件的时分,会出现异常。
FtpWebResponseresponse=(FtpWebResponse)reqFTP.GetResponse();
StreamftpStream=response.GetResponseStream();
StreamReaderreader=newStreamReader(ftpStream);
stringfileStr=reader.ReadToEnd();
reader.Close();
ftpStream.Close();
response.Close();
returnfileStr;
}
catch(Exceptionex)
{
LogHelper.writeErrorLog(“获取ftp文件并读取内容失利:”+ex.Message);
returnnull;
}
}
///
///获取文件巨细
///
///ip服务器下的相对途径
///文件巨细
publicstaticintGetFileSize(stringfile)
{
StringBuilderresult=newStringBuilder();
FtpWebRequestrequest;
try
{
request=(FtpWebRequest)FtpWebRequest.Create(newUri(path+file));
request.UseBinary=true;
request.Credentials=newNetworkCredential(username,password);//设置用户名和暗码
request.Method=WebRequestMethods.Ftp.GetFileSize;
intdataLength=(int)request.GetResponse().ContentLength;
returndataLength;
}
catch(Exceptionex)
{
Console.WriteLine(“获取文件巨细犯错:”+ex.Message);
return-1;
}
}
///
///文件上传
///
///原途径(绝对途径)包含文件名
///方针文件夹:服务器下的相对途径不填为根目录
publicstaticvoidFileUpLoad(stringfilePath,stringobjPath=””)
{
try
{
stringurl=path;
if(objPath!=””)
url+=objPath+”/”;
try
{
FtpWebRequestreqFTP=null;
//待上传的文件(全途径)
try
{
FileInfofileInfo=newFileInfo(filePath);
using(FileStreamfs=fileInfo.OpenRead())
{
longlength=fs.Length;
reqFTP=(FtpWebRequest)FtpWebRequest.Create(newUri(url+fileInfo.Name));
//设置衔接到FTP的帐号暗码
reqFTP.Credentials=newNetworkCredential(username,password);
//设置请求完成后是否坚持衔接
reqFTP.KeepAlive=false;
//指定履行指令
reqFTP.Method=WebRequestMethods.Ftp.UploadFile;
//指定数据传输类型
reqFTP.UseBinary=true;
using(Streamstream=reqFTP.GetRequestStream())
{
//设置缓冲巨细
intBufferLength=5120;
byte[]b=newbyte[BufferLength];
inti;
while((i=fs.Read(b,0,BufferLength))>0)
{
stream.Write(b,0,i);
}
Console.WriteLine(“上传文件成功”);
}
}
}
catch(Exceptionex)
{
Console.WriteLine(“上传文件失利过错为”+ex.Message);
}
finally
{
}
}
catch(Exceptionex)
{
Console.WriteLine(“上传文件失利过错为”+ex.Message);
}
finally
{
}
}
catch(Exceptionex)
{
Console.WriteLine(“上传文件失利过错为”+ex.Message);
}
}
///
///删去文件
///
///服务器下的相对途径包含文件名
publicstaticvoidDeleteFileName(stringfileName)
{
try
{
FileInfofileInf=newFileInfo(ftpip+””+fileName);
stringuri=path+fileName;
FtpWebRequestreqFTP=(FtpWebRequest)FtpWebRequest.Create(newUri(uri));
//指定数据传输类型
reqFTP.UseBinary=true;
//ftp用户名和暗码
reqFTP.Credentials=newNetworkCredential(username,password);
//默以为true,衔接不会被关闭
//在一个指令之后被履行
reqFTP.KeepAlive=false;
//指定履行什么指令
reqFTP.Method=WebRequestMethods.Ftp.DeleteFile;
FtpWebResponseresponse=(FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch(Exceptionex)
{
Console.WriteLine(“删去文件犯错:”+ex.Message);
}
}
///
///新建目录上一级必须先存在
///
///服务器下的相对途径
publicstaticvoidMakeDir(stringdirName)
{
try
{
stringuri=path+dirName;
FtpWebRequestreqFTP=(FtpWebRequest)FtpWebRequest.Create(newUri(uri));
//指定数据传输类型
reqFTP.UseBinary=true;
//ftp用户名和暗码
reqFTP.Credentials=newNetworkCredential(username,password);
reqFTP.Method=WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponseresponse=(FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch(Exceptionex)
{
Console.WriteLine(“创立目录犯错:”+ex.Message);
}
}
///
///删去目录上一级必须先存在
///
///服务器下的相对途径
publicstaticvoidDelDir(stringdirName)
{
try
{
stringuri=path+dirName;
FtpWebRequestreqFTP=(FtpWebRequest)FtpWebRequest.Create(newUri(uri));
//ftp用户名和暗码
reqFTP.Credentials=newNetworkCredential(username,password);
reqFTP.Method=WebRequestMethods.Ftp.RemoveDirectory;
FtpWebResponseresponse=(FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch(Exceptionex)
{
Console.WriteLine(“删去目录犯错:”+ex.Message);
}
}
///
///从ftp服务器上取得文件夹列表
///
///服务器下的相对途径
///
publicstaticListGetDirctory(stringRequedstPath)
{
Liststrs=newList();
try
{
stringuri=path+RequedstPath;//方针途径path为服务器地址
FtpWebRequestreqFTP=(FtpWebRequest)FtpWebRequest.Create(newUri(uri));
//ftp用户名和暗码
reqFTP.Credentials=newNetworkCredential(username,password);
reqFTP.Method=WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponseresponse=reqFTP.GetResponse();
StreamReaderreader=newStreamReader(response.GetResponseStream());//中文文件名
stringline=reader.ReadLine();
while(line!=null)
{
if(line.Contains(“”))
{
stringmsg=line.Substring(line.LastIndexOf(“”)+5).Trim();
strs.Add(msg);
}
line=reader.ReadLine();
}
reader.Close();
response.Close();
returnstrs;
}
catch(Exceptionex)
{
Console.WriteLine(“获取目录犯错:”+ex.Message);
}
returnstrs;
}
///
///从ftp服务器上取得文件列表
///
///服务器下的相对途径
///
publicstaticListGetFile(stringRequedstPath)
{
Liststrs=newList();
try
{
stringuri=path+RequedstPath;//方针途径path为服务器地址
FtpWebRequestreqFTP=(FtpWebRequest)FtpWebRequest.Create(newUri(uri));
//ftp用户名和暗码
reqFTP.Credentials=newNetworkCredential(username,password);
reqFTP.Method=WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponseresponse=reqFTP.GetResponse();
StreamReaderreader=newStreamReader(response.GetResponseStream());//中文文件名
stringline=reader.ReadLine();
while(line!=null)
{
if(!line.Contains(“”))
{
stringmsg=line.Substring(39).Trim();
strs.Add(msg);
}
line=reader.ReadLine();
}
reader.Close();
response.Close();
returnstrs;
}
catch(Exceptionex)
{
Console.WriteLine(“获取文件犯错:”+ex.Message);
}
returnstrs;
}
}
}

未经允许不得转载:IT技术网站 » FTPHelper,FTP公共帮助类同享
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载