C#file Common methods of file operation (create, move, delete, copy, etc.)

Source: Internet
Author: User
Tags file copy

The file class, which is a static class, is mainly used to provide some function libraries. Static utility classes provide a number of static methods that support basic operations on files, including creating, copying, moving, deleting, and opening a file.

The parameters of the file class method are often path paths. Some methods of file can return objects of FileStream and StreamWriter. Can be used in support of them. System.IO.File class and System.IO.FileInfo class

It mainly provides various operations related to the file, which need to refer to the System.IO namespace when used.

First, the file class common operation method

1. Create a File method

Parameter 1: path to the file to be created

File.create (@ "D:\Test\Debug1\ test. txt")

2. Open File Method

Parameter 1: File path to open, Parameter 2: Open file Mode

File.Open (@ "D:\Test\Debug1\ test. txt", filemode.append)

3. Append file method

Parameter 1: File path to append, Parameter 2: Append content

File.appendalltext (@ "D:\Test\Debug1\ test. txt", "haha");

4. Copy File method

Parameter 1: Source file path to copy, Parameter 2: Destination file path after copy, Parameter 3: Overwrite same file name
File.Copy (@ "D:\Test\Debug1\ test. txt", @ "D:\Test\Debug2\ test 1.txt", true);

5. Move File method

Parameter 1: path to the source file to be moved, Parameter 2: Destination file path after move
File.move (@ "D:\Test\Debug1\ test. txt", @ "D:\Test\Debug3\ test 2.txt");

6. Delete File method

Parameter 1: file path to delete
File.delete (@ "D:\Test\Debug1\ test. txt");

7. Set file Properties method

Parameter 1: To set the file path of the property, Parameter 2: Set the property type (read-only, hidden, etc.)
File.setattributes (@ "D:\Test\Debug1\ test. txt", Fileattributes.hidden);

Second, interface and source code examples:

1. Interface layout

2. Source code Example:

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.io;using system.linq;using system.text;using system.threading.tasks;using System.Windows.Forms; namespace filehandletest{public partial class Form1:form {public Form1 () {Initializeco        Mponent ();    #region file Operations Method (create, copy, delete, move, append, open, set property, and so on) of the files class static string path = @ "D:\Test\Debug1\ test. txt";  source file path static string path1 = @ "D:\Test\Debug2\ Test 1.txt";  File copy path static string path2 = @ "D:\Test\Debug3\ Test 2.txt";    File move path static string path3 = @ "C: \ Test 3.txt"; Cross-drive character storage path (test)//<summary>//1, create file method///</summary>//<param name= "sender "></param>//<param name=" E "></param> private void Btncreate_click (object sender, Eve Ntargs e) {//Parameter 1: Specify the file path to be judged if (!        File.exists (PATH))    {//Parameter 1: File path to be created, including file name, suffix, etc. FileStream fs = file.create (path); Fs.                Close (); MessageBox.Show ("File creation succeeded!            "); } else {MessageBox.Show ("The file already exists!            "); }}///<summary>///2, how to open the file///</summary>//<param name= "Sender" & gt;</param>//<param name= "E" ></param> private void Btnopen_click (object sender, Eventar GS e) {if (file.exists (path)) {//Parameter 1: File path to open, Parameter 2: Open file Mode Fil                eSTREAM fs = File.Open (path, filemode.append);                byte array byte[] bytes = {(byte) ' H ', (byte) ' E ', (byte) ' L ', (Byte) ' L ', (byte) ' O '}; Writes file FS through a character stream. Write (bytes, 0, bytes.                Length); Fs.                Close (); MessageBox.Show ("Open and append Hello success!")            "); } else {MessageBox.Show("File does not exist!")            "); }}///<summary>//3, Append file content method///</summary>//<param name= "Sender" ></param>//<param name= "E" ></param> private void Btnappend_click (object sender, even            Targs e) {string appendtext = This.txtContent.Text; if (file.exists (path)) {///Parameter 1: File path to append, Parameter 2: Append content file.appendalltext (path, append                Text); MessageBox.Show ("File Append content succeeded!            "); } else {MessageBox.Show ("file does not exist!            "); }}///<summary>//4, Copy file method (only in the same drive letter)////</summary>//<p Aram Name= "Sender" ></param>//<param name= "E" ></param> private void Btncopy_click (obj ECT sender, EventArgs e) {if (file.exists (path)) {//Parameter 1: Source file path to copy, Parameter 2: Target after replication File path, Parameter 3: whether to overwrite the same textPiece name File.Copy (path, path1, true); MessageBox.Show ("Copy file succeeded!            "); } else {MessageBox.Show ("file does not exist!            "); }}////<summary>//5, Move File method (only in the same drive letter operation)///////<param NA Me= "Sender" ></param>//<param name= "E" ></param> private void Btnmove_click (object sen                Der, EventArgs e) {if (file.exists (path)) {//Parameter 1: path to the source file to be moved, Parameter 2: Destination file path after move                File.move (path, path2); MessageBox.Show ("Move file successfully!")            "); } else {MessageBox.Show ("file does not exist!            "); }}///<summary>//6, Delete file method///</summary>//<param name= "Sender" &G t;</param>//<param name= "E" ></param> private void Btndelete_click (object sender, Eventa  RGS e) {if (file.exists (path))          {//Parameter 1: file path to delete file.delete (path); MessageBox.Show ("File deleted successfully!            "); } else {MessageBox.Show ("file does not exist!            "); }}////<summary>////7, Set file properties method///</summary>//<param name= "Sender" ></param>//<param name= "E" ></param> private void Btnset_click (object sender, Eventar                GS e) {if (path) {///Parameter 1: The file path of the property to be set, Parameter 2: Set the property type (read-only, hidden, and so on) (file.exists)                File.setattributes (path, Fileattributes.hidden); MessageBox.Show ("Set file properties for hidden success!")            "); } else {MessageBox.Show ("file does not exist!            "); }} #endregion}}

Reference Source: http://www.cnblogs.com/mfc-itblog/p/5771780.html

C#file Common methods of file operation (create, move, delete, copy, etc.)

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.