
复制代码 代码如下:
class ftp
{
private string host = null;
private string user = null;
private string pass = null;
private FtpWebRequest ftpRequest = null;
private FtpWebResponse ftpResponse = null;
private Stream ftpStream = null;
private int bufferSize = 2048;
public ftp(string hostIP,string username,string password) { host = hostIP; user = username; pass = password; }
public voID download(string remotefile,string localfile)
{
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remotefile);
ftpRequest.Credentials = new NetworkCredential(user,pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.Downloadfile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
fileStream localfileStream = new fileStream(localfile,fileMode.Create);
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer,bufferSize);
try
{
while (bytesRead > 0)
{
localfileStream.Write(byteBuffer,bytesRead);
bytesRead = ftpStream.Read(byteBuffer,bufferSize);
}
}
catch (Exception ex) { Console.Writeline(ex.ToString()); }
localfileStream.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.Writeline(ex.ToString()); }
return;
}
public voID upload(string remotefile,pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.Uploadfile;
ftpStream = ftpRequest.GetRequestStream();
fileStream localfileStream = new fileStream(localfile,fileMode.Create);
byte[] byteBuffer = new byte[bufferSize];
int bytesSent = localfileStream.Read(byteBuffer,bufferSize);
try
{
while (bytesSent != 0)
{
ftpStream.Write(byteBuffer,bytesSent);
bytesSent = localfileStream.Read(byteBuffer,bufferSize);
}
}
catch (Exception ex) { Console.Writeline(ex.ToString()); }
localfileStream.Close();
ftpStream.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.Writeline(ex.ToString()); }
return;
}
public voID delete(string deletefile)
{
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deletefile);
ftpRequest.Credentials = new NetworkCredential(user,pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.Deletefile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.Writeline(ex.ToString()); }
return;
}
public voID rename(string currentfilenameAndpath,string newfilename)
{
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + currentfilenameAndpath);
ftpRequest.Credentials = new NetworkCredential(user,pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.Rename;
ftpRequest.RenameTo = newfilename;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.Writeline(ex.ToString()); }
return;
}
public voID createDirectory(string newDirectory)
{
try
{
ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + newDirectory);
ftpRequest.Credentials = new NetworkCredential(user,pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.Writeline(ex.ToString()); }
return;
}
public string getfileCreatedDateTime(string filename)
{
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + filename);
ftpRequest.Credentials = new NetworkCredential(user,pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
StreamReader ftpReader = new StreamReader(ftpStream);
string fileInfo = null;
try { fileInfo = ftpReader.ReadToEnd(); }
catch (Exception ex) { Console.Writeline(ex.ToString()); }
ftpReader.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
return fileInfo;
}
catch (Exception ex) { Console.Writeline(ex.ToString()); }
return "";
}
public string getfileSize(string filename)
{
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + filename);
ftpRequest.Credentials = new NetworkCredential(user,pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.GetfileSize;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
StreamReader ftpReader = new StreamReader(ftpStream);
string fileInfo = null;
try { while (ftpReader.Peek() != -1) { fileInfo = ftpReader.ReadToEnd(); } }
catch (Exception ex) { Console.Writeline(ex.ToString()); }
ftpReader.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
return fileInfo;
}
catch (Exception ex) { Console.Writeline(ex.ToString()); }
return "";
}
public string[] directoryListSimple(string directory)
{
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
ftpRequest.Credentials = new NetworkCredential(user,pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
StreamReader ftpReader = new StreamReader(ftpStream);
string directoryRaw = null;
try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.Readline() + "|"; } }
catch (Exception ex) { Console.Writeline(ex.ToString()); }
ftpReader.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
try { string[] directoryList = directoryRaw.Split("|".tochararray()); return directoryList; }
catch (Exception ex) { Console.Writeline(ex.ToString()); }
}
catch (Exception ex) { Console.Writeline(ex.ToString()); }
return new string[] { "" };
}
public string[] directoryListDetailed(string directory)
{
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
ftpRequest.Credentials = new NetworkCredential(user,pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
StreamReader ftpReader = new StreamReader(ftpStream);
string directoryRaw = null;
try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.Readline() + "|"; } }
catch (Exception ex) { Console.Writeline(ex.ToString()); }
ftpReader.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
try { string[] directoryList = directoryRaw.Split("|".tochararray()); return directoryList; }
catch (Exception ex) { Console.Writeline(ex.ToString()); }
}
catch (Exception ex) { Console.Writeline(ex.ToString()); }
return new string[] { "" };
}
}
复制代码 代码如下:
ftp ftpClIEnt = new ftp(@"ftp://10.10.10.10/","user","password");
ftpClIEnt.upload("etc/test.txt",@"C:\Users\Metastruct\Desktop\test.txt");
ftpClIEnt.download("etc/test.txt",@"C:\Users\Metastruct\Desktop\test.txt");
ftpClIEnt.delete("etc/test.txt");
ftpClIEnt.rename("etc/test.txt","test2.txt");
ftpClIEnt.createDirectory("etc/test");
string fileDateTime = ftpClIEnt.getfileCreatedDateTime("etc/test.txt");
Console.Writeline(fileDateTime);
string fileSize = ftpClIEnt.getfileSize("etc/test.txt");
Console.Writeline(fileSize);
string[] simpleDirectoryListing = ftpClIEnt.directoryListDetailed("/etc");
for (int i = 0; i < simpleDirectoryListing.Count(); i++) { Console.Writeline(simpleDirectoryListing[i]);
string[] detailDirectoryListing = ftpClIEnt.directoryListDetailed("/etc");
for (int i = 0; i < detailDirectoryListing.Count(); i++) { Console.Writeline(detailDirectoryListing[i]); }
ftpClIEnt = null;
以上是内存溢出为你收集整理的c# *** 作ftp类分享全部内容,希望文章能够帮你解决c# *** 作ftp类分享所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)