private List <string> GetGeoTiffFiles() { // Example string: ftp://13.78.149.101:21/2018/08/15 string url = FileServerUtil.GetFileServerUrl("13.78.149.101", 21, new DateTime(2018, 8, 15)); FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(url); ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; ftpRequest.Credentials = FileServerUtil.GetFileServerCredential(); FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); string[] directoryOrFileNames; using (Stream responseStream = ftpResponse.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream); string result = reader.ReadToEnd(); directoryOrFileNames = String.IsNullOrEmpty(result) ? null : result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); } List <string> filteredResult = new List <string>(); for (int i = 0; i < directoryOrFileNames.Length; i++) { string fileName = directoryOrFileNames[i]; if (fileName.Contains("_230000.p") && fileName.EndsWith(".tif")) { filteredResult.Add(string.Format("{0}/{1}", url, fileName)); } } return(filteredResult); }
public void DownloadAndSaveFiles() { // Example string: ftp://13.78.149.101:21/subset string serverUrl = FileServerUtil.GetFileServerUrl("13.78.149.101", 21, "subset"); FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(serverUrl); ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; ftpRequest.Credentials = FileServerUtil.GetFileServerCredential(); FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); string[] directoryOrFileNames; using (Stream responseStream = ftpResponse.GetResponseStream()) { StreamReader reader = new StreamReader(responseStream); string result = reader.ReadToEnd(); directoryOrFileNames = String.IsNullOrEmpty(result) ? null : result.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); } List <string> filteredResult = new List <string>(); for (int i = 0; i < directoryOrFileNames.Length; i++) { string fileName = directoryOrFileNames[i]; if (fileName.EndsWith(".zip")) { string serverFileFullPath = string.Format("{0}/{1}", serverUrl, fileName); FileServerUtil.ProcessSourceFileFromFTPServer(serverFileFullPath, @"C:\GmuTemp"); } } }
private void DownloadGeoTiffFile(string fileUrl, string localFilePath) { FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(fileUrl); ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; ftpRequest.Credentials = FileServerUtil.GetFileServerCredential(); FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); using (Stream responseStream = ftpResponse.GetResponseStream()) { using (FileStream fs = File.OpenWrite(localFilePath)) { fs.Seek(0, SeekOrigin.Begin); responseStream.CopyTo(fs); } } }
private void UploadKmzFile(string localFilePath, string fileUrl) { FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(fileUrl); ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; ftpRequest.Credentials = FileServerUtil.GetFileServerCredential(); using (FileStream localFileSteam = File.OpenRead(localFilePath)) { ftpRequest.ContentLength = localFileSteam.Length; using (Stream requestStream = ftpRequest.GetRequestStream()) { localFileSteam.CopyTo(requestStream); } using (FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse()) { Console.WriteLine($"Upload File Complete, status {response.StatusDescription}"); } } }