■■■

2016年4月5日火曜日

FtpWebRequestでFTPサーバーのファイル名を変更する方法

FtpWebRequestでFTPサーバーのファイル名を変更する方法
VB.NET
Dim u As New Uri("ftp://localhost/test.txt")
Dim newName As String = "test2.txt"

Dim ftpReq As System.Net.FtpWebRequest = _
CType(System.Net.WebRequest.Create(u), System.Net.FtpWebRequest)
ftpReq.Credentials = New System.Net.NetworkCredential("username", "password")
ftpReq.Method = System.Net.WebRequestMethods.Ftp.Rename

ftpReq.RenameTo = newName

Dim ftpRes As System.Net.FtpWebResponse = _
CType(ftpReq.GetResponse(), System.Net.FtpWebResponse)

Console.WriteLine("{0}: {1}", ftpRes.StatusCode, ftpRes.StatusDescription)
ftpRes.Close()
C#
Uri u = new Uri("ftp://localhost/test.txt");
string newName = "test2.txt";

System.Net.FtpWebRequest ftpReq = (System.Net.FtpWebRequest)
System.Net.WebRequest.Create(u);
ftpReq.Credentials = new System.Net.NetworkCredential("username", "password");
ftpReq.Method = System.Net.WebRequestMethods.Ftp.Rename;

ftpReq.RenameTo = newName;

System.Net.FtpWebResponse ftpRes =
(System.Net.FtpWebResponse)ftpReq.GetResponse();

Console.WriteLine("{0}: {1}", ftpRes.StatusCode, ftpRes.StatusDescription);
ftpRes.Close();

■■■