Tuesday, June 30, 2009

Read whole text of text file into string

public string mReadFileIntoString(string FullPath, string Separator)
{
string ReturnString = string.Empty;

if (!string.IsNullOrEmpty(FullPath) && !string.IsNullOrEmpty(Separator))
{
if (File.Exists(FullPath))
{
try
{
string Line = string.Empty;

using (FileStream fs = new FileStream(FullPath, FileMode.Open, FileAccess.Read))
{
using (StreamReader rd = new StreamReader(fs, Encoding.Default))
{
while (!rd.EndOfStream)
{
Line = rd.ReadLine();
if (string.IsNullOrEmpty(ReturnString) && !string.IsNullOrEmpty(Line))
ReturnString = Line;
else if (!string.IsNullOrEmpty(ReturnString) && !string.IsNullOrEmpty(Line))
ReturnString = ReturnString + Separator + Line;
}
rd.Close();
}
}

return ReturnString;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("{0} has Input Error, Please Check the Input Files", FullPath);
System.Windows.Forms.MessageBox.Show("Please re-run the application with correct Input");
return string.Empty;
// return string.Format("{0} has Input Error, Please Check the Input Files", FullPath);
}
}
else
{
System.Windows.Forms.MessageBox.Show("{0} does not Exists, Please Check the Input Files", FullPath);
System.Windows.Forms.MessageBox.Show("Please re-run the application with correct Input");
return string.Empty;
// return string.Format("{0} does not Exists, Please Check the Input Files", FullPath);
}
}
return null;
}

No comments:

Post a Comment

 
Locations of visitors to this page