There are a lot of information in the project need to be stored in a text file, such as some introductory information, articles, etc., it is often necessary to read this file Chinese, and returns a string.
The following provides a read file and returns a string method, as follows:
/// <Summary>
/// read file into a string
/// </ Summary>
/// absolute path <param name = "filePath"> file </ param>
public String FileToString (String filePath)
{
return FileToString (filePath, Encoding.GetEncoding ( "GB2312"));
}
/// <Summary>
/// read file into a string
/// </ Summary>
/// <param name = "filePath "> absolute path of the file </ param>
/// <param name =" encoding "> character encoding </ param>
public String FileToString (String filePath,Encoding Encoding)
{
// create a stream reader
StreamReader reader = new StreamReader (filePath, encoding);
strContent = String "";
the try
{
// read the stream
strContent reader.ReadToEnd = ();
}
the catch (Exception EX)
{
debug strings // output
string strOuput = string.Format ( "string to read the file an error occurred, ErrMsg {0}, the InnerException. 1} {\ n-", ex.Message, ex.InnerException);
// write information to the log output file
DllComm.TP_WriteAppLogFileEx (DllComm.g_AppLogFileName, strOuput);
}
the finally
{
// Close stream reader
reader.Close ();
}
return strContent;
}
Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/09/23/2186444.html