How to read CSV file with FTP using C #

I can authenticate, but I'm not sure how I can read the CSV file. I usually contact the hard drive, which I read as follows.

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

But how can I read from ftp. My Ftp path is similar to ftp://ftp.atozfdc.com.au/data.csv Here is my code for transmitting 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();
+4
source share
3 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);
+6
source

Look at the link


TextFieldParseris in the namespace Microsoft.VisualBasic.FileIO. Therefore, you need to add a link toMicrosoft.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 suggest you upload the file to a temporary location, and then use the temp file path to analyze the CSV.

, ResponseStream

:

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
+3

ftp://ftp.atozfdc.com.au/data.csv This is your path to the ftp file. what else are you looking for? if you want to read the file use

StreamReader reader = new StreamReader(responseStream);
string[] allLines = reader.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
0
source

Source: https://habr.com/ru/post/1544682/


All Articles