Writing can be divided into continued writing and overwriting. You only need to change the value of the first parameter to switch.
The code is as follows:
| The code is as follows: |
Copy code |
|
/// <Summary>
/// Write the content to a text file
/// </Summary>
/// <Param name = "count"> status to determine whether to continue or overwrite </param>
/// <Param name = "fileName"> File name </param>
/// <Param name = "content"> content </param>
/// <Returns> the returned error message or 1 indicates that the write is successful. </returns>
Public static string writeFile (bool bl, string fileName, string content)
{
Try
{
String pathName = System. Web. HttpContext. Current. Server. MapPath (fileName );
// This does not exist and will be automatically created
Using (StreamWriter sw = new StreamWriter (pathName, bl, Encoding. Default ))
{
Sw. Write (content );
Sw. Flush ();
Sw. Close ();
}
}
Catch (Exception ex)
{
Return ex. Message;
}
Return "1 ";
}
/// <Summary>
/// Read the text file content
/// </Summary>
/// <Param name = "fileName"> File name </param>
/// <Returns> returned content </returns>
Public static string readFile (string fileName)
{
String result = "";
Try
{
String pathName = System. Web. HttpContext. Current. Server. MapPath (fileName );
Using (StreamReader sr = new StreamReader (pathName, Encoding. Default ))
{
Result = sr. ReadToEnd ();
Sr. Close ();
}
}
Catch (Exception ex)
{
Return "0"; // ex. Message;
}
Return result;
} |