Cagliostro
Programmer
say me please how to read entirely a small ASCII text file in a string variable?
Ion Filipski
Ion Filipski
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
using System;
using System.IO;
using System.Text;
StringBuilder ReadFile(string sPathFile)
{
StreamReader sr = null;
System.Text.StringBuilder sb = null;
try
{
sb = new System.Text.StringBuilder();
sr = new StreamReader(sPathFile);
string line ="";
while ((line = sr.ReadLine()) != null)
{
sb.Append (line);
}
}
catch (Exception e)
{
string sMsg = "Error reading file : " + sPathFile + " into a StringBuilder " + e.GetType() + e.Message;
throw new Exception (sMsg);
}
finnaly
{
if (sr !=null)
sr.Close();
sr = null;
}
return sb;
}
How to use:
StringBuilder sb = ReadFile("C:\\todo.txt");
if (sb !=null)
{
string sTemp = sb.ToString();
}