Using System. Collections. Generic;
Using System. Text;
Using System. Net;
Using System. IO;
/// <Summary>
/// Summary of FtpUpDown
/// </Summary>
Public class FtpUpDown
{
Public FtpUpDown ()
{
//
// TODO: add the constructor logic here
//
}
String ftpServerIP;
String ftpUserID;
String ftpPassword;
FtpWebRequest reqFTP;
# Region // connect to ftp
Private void Connect (String path) // Connect to ftp
{
// Create an FtpWebRequest object based on the uri
ReqFTP = (FtpWebRequest) FtpWebRequest. Create (new Uri (path ));
// Specify the data transmission type
ReqFTP. UseBinary = true;
// Ftp user name and password
ReqFTP. Credentials = new NetworkCredential (ftpUserID, ftpPassword );
}
# Endregion
# Region // assign values
Public FtpUpDown (string ftpServerIP, string ftpUserID, string ftpPassword)
{
This. ftpServerIP = ftpServerIP;
This. ftpUserID = ftpUserID;
This. ftpPassword = ftpPassword;
}
# Endregion
// Call this
Private string [] GetFileList (string path, string WRMethods) // The code above demonstrates how to obtain the file list from the ftp server
{
String [] downloadFiles;
StringBuilder result = new StringBuilder ();
Try
{
Connect (path );
ReqFTP. Method = WRMethods;
Webresponse response = reqftp. getresponse ();
Streamreader reader = new streamreader (response. getresponsestream (), system. Text. encoding. Default); // Chinese file name
String line = reader. Readline ();
While (line! = NULL)
{
Result. append (line );
Result. append ("/N ");
Line = reader. Readline ();
}
// To remove the trailing '/N'
Result. Remove (result. tostring (). lastindexof ('/N'), 1 );
Reader. Close ();
Response. Close ();
Return result. tostring (). Split ('/N ');
}
Catch (exception ex)
{
DownloadFiles = null;
Return downloadFiles;
}
}
Public string [] GetFileList (string path) // The preceding code example shows how to obtain the file list from the ftp server.
{
Return GetFileList ("ftp: //" + ftpServerIP + "/" + path, WebRequestMethods. Ftp. ListDirectory );
}
Public string [] GetFileList () // The code above demonstrates how to obtain the file list from the ftp server
{
Return GetFileList ("ftp: //" + ftpServerIP + "/", WebRequestMethods. Ftp. ListDirectory );
}
Public void Upload (string filename) // the above Code uploads files from the ftp server
{
FileInfo fileInf = new FileInfo (filename );
// String uri = "ftp: //" + ftpServerIP + "/bjoil_qt/Uploads/" + fileInf. Name;
String uri = "ftp: //" + ftpServerIP + "/" + fileInf. Name;
Connect (uri); // Connect
// The default value is true, and the connection will not be closed
// Executed after a command
ReqFTP. KeepAlive = false;
// Specify the command to be executed
Reqftp. method = webrequestmethods. FTP. uploadfile;
// Notify the server of the file size when uploading the file
Reqftp. contentlength = fileinf. length;
// Set the buffer size to kb.
Int bufflength = 2048;
Byte [] buff = new byte [bufflength];
Int contentlen;
// Open a file stream (system. Io. filestream) to read the uploaded file
Filestream FS = fileinf. openread ();
Try
{
// Write the uploaded file to the stream
Stream strm = reqFTP. GetRequestStream ();
// Kb of each file stream read
ContentLen = fs. Read (buff, 0, buffLength );
// The stream content has not ended
While (contentLen! = 0)
{
// Write content from file stream to upload stream
STRM. Write (buff, 0, contentlen );
Contentlen = FS. Read (buff, 0, bufflength );
}
// Close two streams
STRM. Close ();
FS. Close ();
}
Catch (exception ex)
{
}
}
Public bool Download (string filePath, string fileName, out string errorinfo) // the above Code implements the file Download function from the ftp server.
{
Try
{
String onlyFileName = Path. GetFileName (fileName );
String newFileName = filePath + "//" + onlyFileName;
If (File. Exists (newFileName ))
{
Errorinfo = string. Format ("the local file {0} already exists and cannot be downloaded", newFileName );
Return false;
}
String url = "ftp: //" + ftpServerIP + "/" + fileName;
Connect (url); // Connect
ReqFTP. Credentials = new NetworkCredential (ftpUserID, ftpPassword );
FtpWebResponse response = (FtpWebResponse) reqFTP. GetResponse ();
Stream ftpStream = response. GetResponseStream ();
Long cl = response. ContentLength;
Int buffer size = 2048;
Int readCount;
Byte [] buffer = new byte [bufferSize];
ReadCount = ftpStream. Read (buffer, 0, bufferSize );
FileStream outputStream = new FileStream (newFileName, FileMode. Create );
While (readCount> 0)
{
OutputStream. Write (buffer, 0, readCount );
ReadCount = ftpStream. Read (buffer, 0, bufferSize );
}
FtpStream. Close ();
OutputStream. Close ();
Response. Close ();
Errorinfo = "";
Return true;
}
Catch (Exception ex)
{
Errorinfo = string. Format ("unable to download because of {0}", ex. Message );
Return false;
}
}
// Delete an object
Public void DeleteFileName (string fileName)
{
Try
{
FileInfo fileInf = new FileInfo (fileName );
String uri = "ftp: //" + ftpServerIP + "/" + fileInf. Name;
Connect (uri); // Connect
// The default value is true, and the connection will not be closed
// Executed after a command
ReqFTP. KeepAlive = false;
// Specify the command to be executed
ReqFTP. Method = WebRequestMethods. Ftp. DeleteFile;
FtpWebResponse response = (FtpWebResponse) reqFTP. GetResponse ();
Response. Close ();
}
Catch (Exception ex)
{
}
}
// Create a directory
Public void MakeDir (string dirName)
{
Try
{
String uri = "ftp: //" + ftpServerIP + "/" + dirName;
Connect (uri); // Connect
ReqFTP. Method = WebRequestMethods. Ftp. MakeDirectory;
FtpWebResponse response = (FtpWebResponse) reqFTP. GetResponse ();
Response. Close ();
}
Catch (Exception ex)
{
}
}
// Delete the Directory
Public void delDir (string dirName)
{
Try
{
String uri = "ftp: //" + ftpServerIP + "/" + dirName;
Connect (uri); // Connect
ReqFTP. Method = WebRequestMethods. Ftp. RemoveDirectory;
FtpWebResponse response = (FtpWebResponse) reqFTP. GetResponse ();
Response. Close ();
}
Catch (Exception ex)
{
}
}
// Obtain the file size
Public long GetFileSize (string filename)
{
Long fileSize = 0;
Try
{
FileInfo fileInf = new FileInfo (filename );
String uri = "ftp: //" + ftpServerIP + "/" + fileInf. Name;
Connect (uri); // Connect
ReqFTP. Method = WebRequestMethods. Ftp. GetFileSize;
FtpWebResponse response = (FtpWebResponse) reqFTP. GetResponse ();
FileSize = response. ContentLength;
Response. Close ();
}
Catch (Exception ex)
{
}
Return fileSize;
}
// Rename the file
Public void Rename (string currentFilename, string newFilename)
{
Try
{
FileInfo fileInf = new FileInfo (currentFilename );
String uri = "ftp: //" + ftpServerIP + "/" + fileInf. Name;
Connect (uri); // Connect
ReqFTP. Method = WebRequestMethods. Ftp. Rename;
ReqFTP. RenameTo = newFilename;
FtpWebResponse response = (FtpWebResponse) reqFTP. GetResponse ();
// Stream ftpStream = response. GetResponseStream ();
// FtpStream. Close ();
Response. Close ();
}
Catch (Exception ex)
{
}
}
// Get clear files
Public string [] GetFilesDetailList ()
{
Return getfilelist ("ftp: //" + ftpserverip + "/", webrequestmethods. FTP. listdirectorydetails );
}
// Get clear files
Public String [] getfilesdetaillist (string path)
{
Return getfilelist ("ftp: //" + ftpserverip + "/" + path, webrequestmethods. FTP. listdirectorydetails );
}
}
// Page call
Protected void button#click (Object sender, eventargs E)
{
String ftpserverip = "10. 177 .*.*";
String ftpuserid = "hh ";
String ftppassword = "";
FtpUpDown ftpUpDown = new FtpUpDown (ftpServerIP, ftpUserID, ftpPassword );
String url = Server. MapPath ("uploads/title4.gif ");
FtpUpDown. Upload (url );
}