Follow by Email

Wednesday, April 3, 2013

how to read a csv file to return a string in

To read a CSV file to  return a string in :

Below is the function i have created which reads the csv file and copies it to the application temp folder then converts it to text file and then finally returns the string.

Public string CSVTOSTR(string sourcefilepath)

       string readContents;
        FileInfo file = new FileInfo(sourcefilepath);
        string newPath = s + file.Name;
        File.Copy(sourcefilepath, newPath, true);
        System.IO.FileInfo FileInfor = new System.IO.FileInfo(newPath);
        FileInfor.MoveTo(System.IO.Path.ChangeExtension(FileInfor.FullName, ".txt"));

        using (StreamReader streamReader = new StreamReader(FileInfor .FullName, Encoding.UTF8))
            readContents = streamReader.ReadToEnd();

        return readContents;