This operation class is used to delete and modify files and query objects. This class basically completes file operations and implements file operations using the simplest code. The Code is as follows.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;
Namespace Common
{
Public class FileOption
{
Public FileOption ()
{
}
/// <Summary>
/// Obtain the file name by Time
/// </Summary>
/// <Returns> </returns>
Public static string GetFileName ()
{
String filename = DateTime. Now. ToString ("yyyMMddHHmmss ");
Return filename;
}
/// <Summary>
/// Save the file
/// </Summary>
/// <Returns> </returns>
Public static bool SaveFile (string Path, string Strings, string PostfixStr)
{
Try
{
Path + = @ "\" + GetFileName () + "." + PostfixStr;
// If (! System. IO. File. Exists (Path ))
//{
// FileStream f = File. Create (Path );
// F. Close ();
//}
StreamWriter f2 = new StreamWriter (Path, false, System. Text. Encoding. GetEncoding ("gb2312 "));
F2.Write (Strings );
F2.Close ();
F2.Dispose ();
Return true;
}
Catch (Exception ex)
{
Return false;
}
}
// Save the characters to the file
Public static String SaveFileR (string Path, string Strings, string PostfixStr)
{
Try
{
String filename = GetFileName ();
Path + = @ "\" + filename + "." + PostfixStr;
StreamWriter f2 = new StreamWriter (Path, false, System. Text. Encoding. GetEncoding ("gb2312 "));
F2.Write (Strings );
F2.Close ();
F2.Dispose ();
Return filename;
}
Catch (Exception ex)
{
Return "";
}
}
// Read the file content to the string www.2cto.com
Public static string OpenFile (string Path)
{
Return File. ReadAllText (Path );
}
// Obtain all files in a folder
Public static string [] GetFiles (string Path)
{
Return Directory. GetFiles (Path );
}
}
}
All right, all the file operation code has been compiled, and the usage of this class is very simple. You can know how to use it in future development.
Author zhaoyang