C # Development notes for small software (SaveClassic) -- (3) Basic Class (file operation class FileOption)

Source: Internet
Author: User

 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.