File and folder operations

Source: Internet
Author: User

1. File Management)

1). Read files

-> File path

-> File format (gb2312, UTF-8)

Encoding. Default-> encoding applied to the file content (system default encoding)

Encoding. getencoding ("gb2312")-> change to international Encoding

2). All file operations are in system. Io.

-> Read all the text and return a string: readalltext

-> Read the text of each row and return a string array: readalllines

-> Press enter to put the text in the array as an item.

-> Read the text content and use each row of the text content as an item in the array.

-> Read all text into the Array

3). Use the file method to write files. If the file does not exist, it is automatically created. If the file is operated, the file content will be overwritten.

4) file operations:

-> All file operations must reference the system. Io namespace. All file operations are in the file class.

-> Read files

-> Readalllines (path, encoding format);-> returns an array, which is a row-by-row read

-> Readalltext (path, encoding format);-> Returns a string with a linefeed of \ r \ n

-> Write an object

-> Writealltext (path, string content, encoding format );

-> Writealllines (path, string array, encoding format );

-> Append file-> appendalltext

5). File Management

-> Exist judgment

-> Create

-> Move

-> Shard Replication

-> Delete

2. Folder Management (directory) folder management is simpler than file management:

-> 1) deposit determination

-> Directory. Existe (Folder path); Return bool type

-> 2) create

-> Directory. createdirectory (path of the folder to be created );

-> 3) Delete

-> Directory. Delete (the folder to be deleted );

-> 4) mobile

-> Directory. Move (path of the folder to be moved, path to the destination );

3. Problem

Whether the file or folder is moved or copied, the same name may occur, or the file path is not fully written, and the target Folder does not exist.

For example, if you move the D: \ 1.txt file to D: \ 1 \ 1.txt, the following problems may occur:

-> This causes incomplete file paths: file. Move (@ "D: \ 1.txt", @" D: \ 1 ");

-> Solution: complete the path name

-> If disk D does not have a file named 1, the write operation will mean that the target Folder does not exist: file. Move (@ "D: \ 1.txt", @" D: \ 1 \ 1.txt ");

-> Solution: Create folder 1 first.

-> If the 1.txt file is included in the 1st folder, an exception occurs: file. Copy (@ "D: \ 1.txt", @" D: \ 1 \ 1.txt ");

-> Solution: Use try-catch to catch exceptions or overwrite the original file: file. copy (@ "D: \ 1.txt", @" D: \ 1 \ 1.txt", true );

4. Get the files and folders in the folder

Get file:

-> Directory. getfiles (specify the path of the folder, *. suffix (specified file type); returns an array. If no second parameter is specified, the file is returned.

Get the folder:

-> Directory. getdirectory (specify the path of the folder, *. suffix (specified file type); returns an array, instead of a second parameter, returns the folder

Use foreach to traverse the name of a file or folder:

-> Foreach (string item in arrayname (returns the array name ))

{

Int Index = item. lastindexof ('\\');

String STR = item. substring (index + 1 );

Console. Readline (STR );

}

 

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.