string path = "ftp://1.1.1.1/media/times/" + Name + "/test/fileName"; string[] pathsplit = path.ToString().Split('/'); string Firstpath = pathsplit[0] + "/" + pathsplit[1] + "/" + pathsplit[2] + "/" + pathsplit[3]; string SecondPath = Firstpath + "/" + pathsplit[4]; string ThirdPath = SecondPath + "/" + pathsplit[5]; int count = pathsplit.Count(); try { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path); request.Method = WebRequestMethods.Ftp.ListDirectory; request.Credentials = new NetworkCredential("sh", "se"); using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) { // Okay. upload(); } } catch (WebException ex) { try { //create the first directory if its not there //If already there call the catch FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(Firstpath)); createdir.Method = WebRequestMethods.Ftp.MakeDirectory; createdir.Credentials = new NetworkCredential("sh", "se"); createdir.UsePassive = true; createdir.UseBinary = true; createdir.KeepAlive = false; FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse(); Stream ftpStream1 = response1.GetResponseStream(); ftpStream1.Close(); response1.Close(); } catch (Exception e) { try { //create the second directory if its not there //If already there call the catch FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(SecondPath)); createdir.Method = WebRequestMethods.Ftp.MakeDirectory; createdir.Credentials = new NetworkCredential("sh", "se"); createdir.UsePassive = true; createdir.UseBinary = true; createdir.KeepAlive = false; FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse(); Stream ftpStream1 = response1.GetResponseStream(); ftpStream1.Close(); response1.Close(); } catch (Exception el) { try { //create the third directory if its not there //If already there call the catch FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(ThirdPath)); createdir.Method = WebRequestMethods.Ftp.MakeDirectory; createdir.Credentials = new NetworkCredential("sh", "se"); createdir.UsePassive = true; createdir.UseBinary = true; createdir.KeepAlive = false; FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse(); Stream ftpStream1 = response1.GetResponseStream(); ftpStream1.Close(); response1.Close(); } catch { } } } if (ex.Response != null) { FtpWebResponse response = (FtpWebResponse)ex.Response; if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { // Directory not found. } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)