Add reference in your class:
using System.Net;
Then copy below mentioned method
/// <summary>
/// GetServerAvailability to check ftp server is available or not
/// </summary>
/// <param name="userId">userId</param>
/// <param name="password">password</param>
/// <param name="ftpServerPath">ftpServerPath</param>
/// <returns>status</returns>
public static bool GetServerAvailability(string userId, string password, string ftpServerPath)
{
FtpWebRequest reqFTP;
bool isServerConnected = false;
try
{
String serverUrl = String.Format("{0}{1}", @"ftp:", ftpServerPath);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(serverUrl));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(userId, password);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
using (WebResponse response = reqFTP.GetResponse())
{
isServerConnected = true;
}
}
catch (Exception ex)
{
if (string.Compare(ex.GetType().ToString(), "System.Net.Sockets.SocketException", true) == 0)
{
System.Net.Sockets.SocketException socketError = (System.Net.Sockets.SocketException)ex;
if (socketError.ErrorCode == 11004)
isServerConnected = false;
}
else
{
isServerConnected = false;
}
}
finally
{
reqFTP = null;
}
return isServerConnected;
}
Above mentioned code will return you true if server is available else false.
using System.Net;
Then copy below mentioned method
/// <summary>
/// GetServerAvailability to check ftp server is available or not
/// </summary>
/// <param name="userId">userId</param>
/// <param name="password">password</param>
/// <param name="ftpServerPath">ftpServerPath</param>
/// <returns>status</returns>
public static bool GetServerAvailability(string userId, string password, string ftpServerPath)
{
FtpWebRequest reqFTP;
bool isServerConnected = false;
try
{
String serverUrl = String.Format("{0}{1}", @"ftp:", ftpServerPath);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(serverUrl));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(userId, password);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
using (WebResponse response = reqFTP.GetResponse())
{
isServerConnected = true;
}
}
catch (Exception ex)
{
if (string.Compare(ex.GetType().ToString(), "System.Net.Sockets.SocketException", true) == 0)
{
System.Net.Sockets.SocketException socketError = (System.Net.Sockets.SocketException)ex;
if (socketError.ErrorCode == 11004)
isServerConnected = false;
}
else
{
isServerConnected = false;
}
}
finally
{
reqFTP = null;
}
return isServerConnected;
}
Above mentioned code will return you true if server is available else false.
No comments:
Post a Comment