Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a CSV file from FTP using C#

Tags:

c#

.net

csv

c#-3.0

ftp

I am able to pass the authentication but i am not sure how can i read a CSV file. Link normally from a hard drive i am reading like this .

string[] allLines = System.IO.File.ReadAllLines(@"D:\CSV\data.csv");

But how can i read from the ftp . My Ftp path is like that ftp://ftp.atozfdc.com.au/data.csv Here is my code to pass ftp authentication.

String ftpserver = "ftp://ftp.atozfdc.com.au/data.csv";
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpserver));
reqFTP.UsePassive = false;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("username", "password");
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
like image 526
user3718016 Avatar asked Jun 16 '14 02:06

user3718016


2 Answers

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
//use the response like below
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string[] allLines = reader.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
like image 124
Damith Avatar answered Oct 22 '22 08:10

Damith


Have a look at this link


TextFieldParser is in the Microsoft.VisualBasic.FileIO namespace. Hence you need to add reference to Microsoft.VisualBasic.dll

String path = @"D:\CSV\data.csv";

using (TextFieldParser parser = new TextFieldParser(path))
{
    parser.SetDelimiters(new string[] { "," });
    parser.HasFieldsEnclosedInQuotes = true;

    // if you want to skip over header line., uncomment line below
    // parser.ReadLine();

    while (!parser.EndOfData)
    {
        string[] fields = parser.ReadFields();
        column1 = fields[0];
        column2 = fields[1];
        column3 = int.Parse(fields[2]);
        column4 = double.Parse(fields[3]);
    }
}

I would suggest you to download the file to a temporary location, and then use the temp file path to parse the CSV.

but if you do not want to create temp file then try using the ResponseStream

Example:

String ftpserver = "ftp://ftp.atozfdc.com.au/data.csv";
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpserver));
reqFTP.UsePassive = false;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("username", "password");
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();


Stream responseStream = response.GetResponseStream();
// use the stream to read file from remote location

using (TextFieldParser parser = new TextFieldParser(responseStream))
{
    // usual csv reader implementation
}

responseStream.Close();
response.Close(); //Closes the connection to the server
like image 34
Parimal Raj Avatar answered Oct 22 '22 06:10

Parimal Raj