異世界


2016年6月22日 星期三

[C#] FTP 下載、 上載和刪除檔案

FTP 下載、 上載和刪除檔案

--------------------------------------------------------------------------------------------------
1.   File Download From FTP:
string localPath = @"G:\FTPTrialLocalPath\";
string fileName = "arahimkhan.txt";

FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source/" + fileName);
requestFileDownload.Credentials = new NetworkCredential("khanrahim", "arkhan22");
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;

FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();

Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create); 

int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length); 

while (bytesRead > 0)
{
 writeStream.Write(buffer, 0, bytesRead);
 bytesRead = responseStream.Read(buffer, 0, Length);
} 

responseStream.Close();
writeStream.Close();

requestFileDownload = null;
responseFileDownload = null;




2. File Upload to FTP:

string localPath = @"G:\FTPTrialLocalPath\";
string fileName = "arahimkhan.txt";

FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1/Destination/" +fileName);
requestFTPUploader.Credentials = new NetworkCredential("khanrahim", "arkhan22");
requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;

FileInfo fileInfo = new FileInfo(localPath + fileName);
FileStream fileStream = fileInfo.OpenRead();

int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];

Stream uploadStream = requestFTPUploader.GetRequestStream();
int contentLength = fileStream.Read(buffer, 0, bufferLength);

while (contentLength != 0)
{
 uploadStream.Write(buffer, 0, contentLength);
 contentLength = fileStream.Read(buffer, 0, bufferLength);
}

uploadStream.Close();
fileStream.Close();

requestFTPUploader = null;
          




3. File Delete From FTP:

string fileName = "arahimkhan.txt";

FtpWebRequest requestFileDelete = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source/" + fileName);
requestFileDelete.Credentials = new NetworkCredential("khanrahim", "arkhan22");
requestFileDelete.Method = WebRequestMethods.Ftp.DeleteFile;

FtpWebResponse responseFileDelete = (FtpWebResponse)requestFileDelete.GetResponse();
          




4. Retrieve File List from FTP Directory:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source");
request.Credentials = new NetworkCredential("khanrahim", "arkhan22");
request.Method = WebRequestMethods.Ftp.ListDirectory;

StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());

string fileName = streamReader.ReadLine();

while (fileName != null)
{
 Console.Writeline(fileName );
 fileName = streamReader.ReadLine();
}

request = null;
streamReader = null;
          



5. Retrieve File List from Local Directory:

string localPath = @"G:\FTPTrialLocalPath\";

string[] files = Directory.GetFiles(localPath);

foreach (string filepath in files)
{
 string fileName = Path.GetFileName(filepath);
 Console.WriteLine(fileName);
}

沒有留言:

張貼留言