Detailed file manipulation methods for the File class in C

  • 2020-12-10 00:49:18
  • OfStack

This article gives an example of the file manipulation methods of the File class in C#. Share to everybody for everybody reference. The specific analysis is as follows:

File is a static class that provides a library of functions. Static utility class, which provides many static methods, supports basic operations on files, including creating, copying, moving, deleting, and opening a file. The parameter of the File class method is often the path path. Some methods of File can return objects of FileStream and StreamWriter. It can be used with them.

The System.IO.File class and the System.IO.FileInfo class provide a variety of operations on files that need to refer to the System.IO namespace when used. The main properties and methods are described by a program example.

(1) File opening method: ES23en. Open ()

The method is declared as follows:

public static FileStream Open(string path,FileMode mode)

The following code is opened and stored in the c:\tempuploads directory named newFile.txt file and written to hello in the file.

private void OpenFile()
{
 FileStream.TextFile=File.Open(@"c:\tempuploads\newFile.txt",FileMode.Append);
 byte [] Info = {(byte)'h',(byte)'e',(byte)'l',(byte)'l',(byte)'o'};
 TextFile.Write(Info,0,Info.Length);
 TextFile.Close();
}

(2) File creation method: ES40en. Create()

The method is declared as follows:

public static FileStream Create(string path;)

The following code demonstrates how to create a file named newFile.txt under c:\tempuploads.
Since the File.Create method grants full read/write access to the new file to all users by default, the file is opened with read/write access and must be closed before it can be opened by other applications. To do this, you need to close the file you created using the Close method of the FileStream class.

private void MakeFile()
{  
    FileStream NewText=File.Create(@"c:\tempuploads\newFile.txt");
 NewText.Close();
}

(3) File deletion method: ES61en. Delete()

The method is declared as follows:

public static void Delete(string path);

The following code demonstrates how to delete the ES71en.txt file in the c:\tempuploads directory.

private void DeleteFile()
{
 File.Delete(@"c:\tempuploads\newFile.txt");
}

(4) File copy method: ES77en.Copy

The method is declared as follows:

public static void Copy(string sourceFileName,string destFileName,bool overwrite);

Copy c:\tempuploads\ newFile.txt to c:\tempuploads\ BackUp.txt with the code below.
Since the OverWrite parameter of the Cope method is set to true, if the BackUp. txt file already exists, it will be overwritten by the copied past file.

private void CopyFile()
{
 File.Copy(@"c:\tempuploads\newFile.txt",@"c:\tempuploads\BackUp.txt",true);
}

(5) File movement method: ES103en.Move

The method is declared as follows:

public static void Move(string sourceFileName,string destFileName); 

The following code moves the ES113en.txt file under c:\tempuploads to the c root directory.

Note:

File transfers can only be performed under the same logical disk. An error will occur if you attempt to transfer a file from the c disk to the d disk.

private void MoveFile()
{
 File.Move(@"c:\tempuploads\BackUp.txt",@"c:\BackUp.txt");
}

(6) Setting file attribute method: ES126en.SetAttributes

The method is declared as follows:

public static void SetAttributes(string path,FileAttributes fileAttributes);

The following code sets the properties of the file c:\tempuploads\ newFile.txt to be read-only and hidden.

private void SetFile()
{
 File.SetAttributes(@"c:\tempuploads\newFile.txt",
 FileAttributes.ReadOnly|FileAttributes.Hidden);
}

In addition to the commonly used read-only and hidden properties of files, there are Archive(file archive status), System(system file), Temporary(temporary file), and so on. For details on file properties, see the description of FileAttributes in MSDN.

(7) The method to determine whether the file exists: ES149en.Exist

The method is declared as follows:

public static bool Exists(string path);

The following code determines the existence of the c:\tempuploads\ newFile.txt file. If it exists, first copy the file, then delete it, and finally move the copied file; If it does not exist, the file is first created, then opened and written, and finally the file properties are set to read-only and hidden.

if(File.Exists(@"c:\tempuploads\newFile.txt")) // Determine if the file exists 
{
 CopyFile(); // Copy the file
 DeleteFile(); // Delete the file
 MoveFile(); // Move files
}
else
{
 MakeFile(); // Generate the file
 OpenFile(); // Open the file
 SetFile(); // Set file properties
}

In addition, the File class provides more support for Text text.

· AppendText: Appends text to an existing file
· CreateText: Creates or opens a new file for writing text
· OpenText: Open an existing text file for reading

However, the above method mainly operates on the encoded text of UTF-8, so it is not flexible enough. It is recommended that you use the following code to manipulate the txt file.
· "read" txt file, the sample code is as follows:

StreamReader TxtReader = new StreamReader(@"c:\tempuploads\newFile.txt",System.Text.Encoding.Default);
string FileContent;
FileContent = TxtReader.ReadEnd();
TxtReader.Close();

· "write" the txt file. The sample code is as follows:

StreamWriter = new StreamWrite(@"c:\tempuploads\newFile.txt",System.Text.Encoding.Default);
string FileContent;
TxtWriter.Write(FileContent);
TxtWriter.Close();

Hopefully this article has helped you with your C# programming.


Related articles: