Created
August 6, 2015 18:07
-
-
Save money4honey/5eea8c9edd8a9c825a0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace myClasses | |
{ | |
public class FtpMaster | |
{ | |
public static void uploadToFtp(string ftpUsername, string ftpPassword, string filePath, string ftpPath) | |
{ | |
try | |
{ | |
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath); | |
request.Method = WebRequestMethods.Ftp.UploadFile; | |
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword); | |
StreamReader sourceStream = new StreamReader(filePath); | |
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); | |
sourceStream.Close(); | |
request.ContentLength = fileContents.Length; | |
Stream requestStream = request.GetRequestStream(); | |
requestStream.Write(fileContents, 0, fileContents.Length); | |
requestStream.Close(); | |
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); | |
response.Close(); | |
} | |
catch { } | |
} | |
public static string dowloadFromFtp(string ftpUsername, string ftpPassword, string ftpPath) | |
{ | |
try | |
{ | |
string fileText; | |
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath); | |
request.Method = WebRequestMethods.Ftp.DownloadFile; | |
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword); | |
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); | |
Stream responseStream = response.GetResponseStream(); | |
StreamReader reader = new StreamReader(responseStream); | |
fileText = Convert.ToString(reader.ReadToEnd()); | |
reader.Close(); | |
response.Close(); | |
return fileText; | |
} | |
catch { | |
return null; | |
} | |
} | |
public static string[] downloadLines(string ftpUsername, string ftpPassword, string ftpPath) { | |
try { | |
List<string> list = new List<string>(); | |
string ftpText = dowloadFromFtp(ftpUsername, ftpPassword, ftpPath); | |
ftpText = ftpText.Replace(";", "[dc]").Replace("\r\n", ";"); | |
string[] array = ftpText.Split(';'); | |
foreach (string item in array) { | |
if (item != "") list.Add(item.Replace("[dc]", ";")); | |
} | |
return list.ToArray(); | |
} | |
catch { | |
return null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment