asp.net (C #) file Operation function Encyclopedia

Source: Internet
Author: User
Tags readfile

For file flow operations, first you have to refer to namespaces: using System.IO; The operation of the file mainly refers to two aspects: first, the operation of the file itself, and second, the contents of the file to operate.

If the former, the landlord can use System.IO.FileInfo and other types, the file to operate, the latter can be system.io.streamreader,streamwriter,filestreamd and other flow objects to the contents of the file to operate.

  asp.net (C #) methods for file manipulation (read, delete, bulk copy, delete ...)

Using System.Data;

Using System.Configuration;

Using System.Web;

Using System.Web.Security;

Using System.Web.UI;

Using System.Web.UI.WebControls;

Using System.Web.UI.WebControls.WebParts;

Using System.Web.UI.HtmlControls;

Using System.Text;

Using System.IO;

  Namespace EC

{

///

Summary description of Fileobj

///

public class Fileobj

{

Constructors

IDisposable Members

Get file suffix name

#region Write a file

/****************************************

* Function Name: WriteFile

* Function Description: When the file is not saved, then create the file, and append files

* Parameter: Path: File path, Strings: text content

* Call Columns:

* String Path = Server.MapPath ("default2.aspx");

* String Strings = "This is what I write";

* EC. Fileobj.writefile (path,strings);

*****************************************/

///

Write a file

///

File path

File contents

public static void WriteFile (String Path, String Strings)

{

if (! System.IO.File.Exists (Path))

{

Directory.CreateDirectory (Path);

System.IO.FileStream f = System.IO.File.Create (Path);

F.close ();

F.dispose ();

}

System.IO.StreamWriter F2 = new System.IO.StreamWriter (Path, True, System.Text.Encoding.UTF8);

F2. WriteLine (Strings);

F2. Close ();

F2. Dispose ();

}

#endregion

#region Read files

/****************************************

* Function Name: ReadFile

* Function Description: Read text content

* Parameters: Path: File paths

* Call Columns:

* String Path = Server.MapPath ("default2.aspx");

* string s = EC. Fileobj.readfile (Path);

*****************************************/

///

Read files

///

File path

///

public static string ReadFile (String Path)

{

string s = "";

if (! System.IO.File.Exists (Path))

s = "There is no corresponding directory";

Else

{

StreamReader F2 = new StreamReader (Path, System.Text.Encoding.GetEncoding ("gb2312"));

s = F2. ReadToEnd ();

F2. Close ();

F2. Dispose ();

}

return s;

}

#endregion

#region Append Files

/****************************************

* Function Name: Fileadd

* Function Description: Append file contents

* Parameter: Path: File path, strings: Content

* Call Columns:

* String Path = Server.MapPath ("default2.aspx");

* String Strings = "new additions";

* EC. Fileobj.fileadd (Path, Strings);

*****************************************/

///

Append files

///

File path

Content

public static void Fileadd (String Path, String strings)

{

StreamWriter sw = File.appendtext (Path);

Sw. Write (strings);

Sw. Flush ();

Sw. Close ();

Sw. Dispose ();

}

#endregion

#region Copy Files

/****************************************

* Function Name: filecoppy

* Function Description: Copy file

* Parameter: Orignfile: Original file, NewFile: New file path

* Call Columns:

* String orignfile = Server.MapPath ("default2.aspx");

* String NewFile = Server.MapPath ("default3.aspx");

* EC. Fileobj.filecoppy (Orignfile, NewFile);

*****************************************/

///

Copy files

///

Original file

New file path

public static void Filecoppy (String orignfile, String NewFile)

{

File.Copy (Orignfile, NewFile, true);

}

#endregion

#region Delete Files

/****************************************

* Function Name: Filedel

* Function Description: Delete file

* Parameters: Path: File paths

* Call Columns:

* String Path = Server.MapPath ("default3.aspx");

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.