Application of File class in C #

Source: Internet
Author: User

Here are a few of the things that are commonly used:

Create: Typically use this overloaded method,file.create (String) , string is a pathname that represents the full path of the file, and the return value is a FileStream instance;

Copy: Copying an existing file to a new file , there are two overloaded methods,file.copy (String, String) the first parameter is the source file, the second parameter is the destination file, and the file with the same name is not allowed to be overwritten. file.copy (String, String, Boolean) , which allows overwriting of files of the same name, can be set to Boolean. when you perform a copy operation, close the source file first, or you will get an error. At the same time, the second parameter is a new file, which originally does not exist, and if it existed, it would be an error.

Delete: Deletes the specified file. If the specified file does not exist, no exception is thrown.

Open: One of the overloaded methods,File.Open (String, FileMode) opens FileStreamon the specified path in FileMode mode with read/write access. Note that the FileMode here is an enumeration type.

For the above four functions, you can use the following small program to understand:

Using System;
Using System.IO;

Class Test
{
public static void Main ()
{
String path = "Lena.raw";
FileStream fs=file.create (path);//Use creat to create a new file
Fs. Close ();
File.Copy (Path, "Copy_lena.raw");
File.Open ("Copy_lena.raw", FileMode.Open);
File.move ("Lena.raw", @ "D:\new_lena.raw");
File.delete (path);
}
}

The rest can be found in the following list:

To perform this action ...

See the examples in this topic ...

Create a text file.

How to: Write Text to a file

Writes a text file.

How to: Write Text to a file

Reads a text file.

How to: Read Text from a file

Appends text to the file.

How to: Open and append to a log file

File.appendtext

Fileinfo.appendtext

Copy the file.

File.Copy

Fileinfo.copyto

Rename or move the file.

File.move

Fileinfo.moveto

Delete the file.

File.delete

Fileinfo.delete

Reads a binary file.

How to: Read and write to a new data file

Writes a binary file.

How to: Read and write to a new data file

Create a directory.

CreateDirectory

Directory

Application of File class in C #

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.