Wednesday, April 3, 2013

how to read a csv file to return a string in asp.net

To read a CSV file to  return a string in asp.net :

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;
}



No comments:

Post a Comment