I know this is an old thread, but I thought I would throw in my 2 cents. In the past I had the issue of getting success or fail back from an FTP server and nothing I could find really worked better than what I pulled from a test program I was using to create my actual application. It seems the big problem comes from getting a response on success or fail when trying to create a folder. The other issue comes since there are multiple Exception types. I have got both WebException
and Exception
at different points so I hence use a general Exception
in my code. It is what works for me. The minor change from the OP is the use of (FtpWebRequest)WebRequest
over what they had.
public static bool CreateFolder(string folder)
{
bool success = false;
System.Net.FtpWebRequest ftp_web_request = null;
System.Net.FtpWebResponse ftp_web_response = null;
string ftp_path = @"ftp://foo.bar.com/" + folder;
try
{
ftp_web_request = (FtpWebRequest)WebRequest.Create(ftp_path);
ftp_web_request.Method = WebRequestMethods.Ftp.MakeDirectory;
ftp_web_request.Credentials = new NetworkCredential("username", "password");
ftp_web_response = (FtpWebResponse)ftp_web_request.GetResponse();
string ftp_response = ftp_web_response.StatusDescription;
string status_code = Convert.ToString(ftp_web_response.StatusCode);
ftp_web_response.Close();
success = true;
}
catch (Exception Ex)
{
string status = Convert.ToString(Ex);
MessageBox.Show("Failed to create folder." + Environment.NewLine + status); //debug
}
return success;
}
The following is what I use to upload multiple files. I pass in a Dictionary
where the key is the name of the file and the value is the path.
public static bool UploadFile(string folder, Dictionary<string, string> Photo_Paths)
{
bool success = false;
FtpWebRequest ftp_web_request = null;
FtpWebResponse ftp_web_response = null;
foreach (KeyValuePair<string, string> item in Photo_Paths)
{
string subdomain = ConfigurationManager.AppSettings["subdomain"];
string ftp_path = @"ftp://foo.bar.com/" + folder + @"/" + item.Key;
try
{
ftp_web_request = (FtpWebRequest)WebRequest.Create(ftp_path);
ftp_web_request.UseBinary = true;
ftp_web_request.UsePassive = false;
ftp_web_request.EnableSsl = false;
ftp_web_request.Method = WebRequestMethods.Ftp.UploadFile;
ftp_web_request.Credentials = new NetworkCredential("username", "password");
try
{
MessageBox.Show(item.Value); //debug
byte[] buffer = File.ReadAllBytes(item.Value);
using (Stream file_stream = ftp_web_request.GetRequestStream())
{
file_stream.Write(buffer, 0, buffer.Length);
}
ftp_web_response = (FtpWebResponse)ftp_web_request.GetResponse();
if (ftp_web_response != null)
{
string ftp_response = ftp_web_response.StatusDescription;
string status_code = Convert.ToString(ftp_web_response.StatusCode);
MessageBox.Show(ftp_response + Environment.NewLine + status_code); //debug
}
}
catch (Exception Ex) //(WebException Ex)
{
//string status = ((FtpWebResponse)Ex.Response).StatusDescription;
string status = Convert.ToString(Ex);
MessageBox.Show("Failed upload a file." + Environment.NewLine + status); //debug
}
ftp_web_response.Close();
success = true;
}
catch (Exception Ex)
{
//string status = ((FtpWebResponse)Ex.Response).StatusDescription;
string status = Convert.ToString(Ex);
if (ftp_web_response != null)
{
string ftp_response = ftp_web_response.StatusDescription;
string status_code = Convert.ToString(ftp_web_response.StatusCode);
MessageBox.Show(ftp_response + Environment.NewLine + status_code); //debug
}
MessageBox.Show("Failed upload a file." + Environment.NewLine + status); //debug
}
}
return success;
}