In this example, i have told you how to Rename file name on FTP server. FTP (File Transfer protocol) is used to transfer a file on another server. To Rename File name on FTP server you must have IP Address, username and password to connect to FTP server.Let's start.
Namespace required to work FtpWebRequest and Stream Class.
C#
using System.Net;
using System.IO;
VB.NET
Imports System.Net
Imports System.IO
Rename Filename on FTP Server function
C#
private void RenameFileName(string currentFilename, string newFilename)
{
FTPSettings.IP = "DOMAIN NAME";
FTPSettings.UserID = "USER ID";
FTPSettings.Password = "PASSWORD";
FtpWebRequest reqFTP = null;
Stream ftpStream = null ;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPSettings.IP + "/" + currentFilename));
reqFTP.Method = WebRequestMethods.Ftp.Rename;
reqFTP.RenameTo = newFilename;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
if (ftpStream != null)
{
ftpStream.Close();
ftpStream.Dispose();
}
throw new Exception(ex.Message.ToString());
}
}
public static class FTPSettings
{
public static string IP { get; set; }
public static string UserID { get; set; }
public static string Password { get; set; }
}
VB.NET
Private Sub RenameFileName(ByVal currentFilename As String, ByVal newFilename As String)
FTPSettings.IP = "DOMAIN NAME"
FTPSettings.UserID = "USER ID"
FTPSettings.Password = "PASSWORD"
Dim reqFTP As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Try
reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + FTPSettings.IP + "/" + currentFilename)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.Rename
reqFTP.RenameTo = newFilename
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(FTPSettings.UserID, FTPSettings.Password)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
ftpStream.Close()
response.Close()
Catch ex As Exception
If ftpStream IsNot Nothing Then
ftpStream.Close()
ftpStream.Dispose()
End If
Throw New Exception(ex.Message.ToString())
End Try
End Sub
Public NotInheritable Class FTPSettings
Private Sub New()
End Sub
Public Shared Property IP() As String
Get
Return m_IP
End Get
Set(ByVal value As String)
m_IP = Value
End Set
End Property
Private Shared m_IP As String
Public Shared Property UserID() As String
Get
Return m_UserID
End Get
Set(ByVal value As String)
m_UserID = Value
End Set
End Property
Private Shared m_UserID As String
Public Shared Property Password() As String
Get
Return m_Password
End Get
Set(ByVal value As String)
m_Password = Value
End Set
End Property
Private Shared m_Password As String
End Class
Function to used as
C#
RenameFileName("file1.txt","file2.text");
VB.NET
RenameFileName("file1.txt","file2.text")
You can use this function on both website and desktop Application
Download
FTP Rename FileName.zip.zip (1.10 kb)