, , , 2 . FTP-, , , , , , . , . , . WebException, Exception , Exception. , . - (FtpWebRequest)WebRequest , .
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);
}
return success;
}
- , . Dictionary, - , - .
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);
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);
}
}
catch (Exception Ex)
{
string status = Convert.ToString(Ex);
MessageBox.Show("Failed upload a file." + Environment.NewLine + status);
}
ftp_web_response.Close();
success = true;
}
catch (Exception Ex)
{
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);
}
MessageBox.Show("Failed upload a file." + Environment.NewLine + status);
}
}
return success;
}