public static string GetDatafromFTP(string filename, string id) { string HOST_NAME = "-"; string USER_NAME = "-"; string PASSWORD = "******"; CommonDBHelper Repo = CommonDBHelper.Instance; List <FTPCredential> data = Repo.GetFtpCredential(id, "GetFtpDownloadCredential").ToList(); foreach (FTPCredential row in data) { HOST_NAME = row.HOST_NAME; USER_NAME = row.USER_NAME; PASSWORD = row.PASSWORD; } NetworkCredential cred = new NetworkCredential(USER_NAME, PASSWORD); WebClient request = new WebClient(); request.Credentials = new NetworkCredential(USER_NAME, PASSWORD); List <DirectoryItem> listing = GetDirectoryInformation(HOST_NAME, USER_NAME, PASSWORD);//"ftp://ftp.mywebsite.com/directory", "username", "password"); string name = listing[0].Name; byte[] filedata = request.DownloadData(HOST_NAME + name); //Path.GetFileName(filename)); using (FileStream file = File.Create(filename + name)) { file.Write(filedata, 0, filedata.Length); file.Close(); } return(filename + name); }
public static void SendDatatoFTP(string localPath, string id) { string HOST_NAME = "-"; string USER_NAME = "-"; string PASSWORD = "******"; CommonDBHelper Repo = CommonDBHelper.Instance; List <FTPCredential> data = Repo.GetFtpCredential(id, "GetFtpCredential").ToList(); foreach (FTPCredential row in data) { HOST_NAME = row.HOST_NAME; USER_NAME = row.USER_NAME; PASSWORD = row.PASSWORD; } NetworkCredential cred = new NetworkCredential(USER_NAME, PASSWORD); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(HOST_NAME + Path.GetFileName(localPath)); request.Proxy = new WebProxy(); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential(USER_NAME, PASSWORD); StreamReader source = new StreamReader(localPath); byte[] fileContent = Encoding.UTF8.GetBytes(source.ReadToEnd()); source.Close(); request.ContentLength = fileContent.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContent, 0, fileContent.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); response.Close(); File.Delete(localPath); }