Path/directory/File action examples-Practical tips for Path class and directory class and the file class

Source: Internet
Author: User
Tags parent directory static class
Path strings are manipulated by path
Get suffix
Can merge paths
Get file name

Directory operations by directory and DirectoryInfo
To determine whether a directory exists
Create a table of contents
Delete Directory
Get all subdirectories under directory
Get all the subfolders under the directory

File and FileInfo to manipulate files
Read files
Write a file
Append files
To determine whether a file exists
Create a file
deleting files

1. Path class
Copy Code code as follows:

Using System;
namespaces for using system.io;//directory and file operations
Namespace _11_path Class {
Class Program {
static void Main (string[] args) {
String path = "C:\\abc\\1.txt";
Note Here is the operation of the path string instead of the real file "modify" support string level, no really renamed file
Path = path.changeextension (path, "avi")//changeextension () modify file suffix Name 1.avi C:\\abc\\1.avi
Combining two paths into one path is better than using +, so it is convenient to solve the problem of whether or not to add a slash, and handle the path separator automatically.
Path = Path.Combine ("c:\\abc\\def\\", "1.jpg"); C:\abc\def\1.jpg
The location of the folder where the file is located is also handled from the angle of the string
Path = path.getdirectoryname (path);//c:\abc
Path = path.getextension (path);//extension. txt
Path = path.getfilename (path);//filename. suffix name 1.txt
Path = path.getfilenamewithoutextension (path);//filename with no suffix 1
Path = Path.GetFullPath ("11-path class. exe");//File full path (usually not in the same way as the full path of a file) F:\PIZIYIMAO\11-Path class \bin\debug\11-path class. exe
Path = Path.gettempfilename ();//temporary folder save path automatically create file C:\Documents and settings\piziyimao\local settings\temp\tmp5e.tmp
Path = Path.gettemppath ()//Get temporary folder save path C:\Documents and settings\piziyimao\local settings\temp\
Console.WriteLine (path);
Console.read ();
}
}
}

2, Operation directory category directory and DirectoryInfo
Copy Code code as follows:

Using System;
Using System.IO;
Namespace _12_directory {
Class Program {
static void Main (string[] args) {
DirectoryInfo dic = new DirectoryInfo ("C:\\abc");
Dic. Name; Get file name
Dic. FullName; Getting file full-path functionality is a powerful difference from directory because it is an instance class and the latter is a static class
Directory. CreateDirectory ("C:\\abc"); Create a folder
Directory. CreateDirectory ("C:\\abc\\1\\2\\3\\4\\5\\6\\7"); Continuously create multilevel folders
if (Directory). Exists ("C:\\ABC"))//Determine if a folder exists
{
Directory. Delete ("C:\\abc"); If it is present, delete if the folder is empty and the normal delete is not empty, the error "Directory is not Empty"
Directory. Delete ("C:\\abc", true); True specifies that the delete operation is also performed if the folder is not empty
}
string [] paths = Directory. GetDirectories ("C:\\abc"); To get all the subdirectory names in the directory note that only one level is removed c:\abc\1 This method is available for all folder paths under the Windows folder
string [] Paths2 = Directory. GetDirectories ("C:\\Windows", "$*"); The above method overload implementation retrieves a file that begins with $
string [] Paths3 = Directory. GetDirectories ("C:\\abc", "*", searchoption.) alldirectories);//wildcard find eligible files in a folder including child folders
foreach (string path in paths) {
Console. WriteLine (path);
}
string [] files = Directory. GetFiles ("C:\\Windows"); Traverse all files under a folder
string [] Files2 = Directory. GetFiles ("C:\\Windows", "*.ini", SearchOption. alldirectories);//Wildcard lookup directory for file usage similar to GetDirectories
foreach (string file in Files2) {
Console. WriteLine (file);
}
The most important thing about directory operations is the GetFiles and GetDirectories methods
Directory. GetParent ("C:\\abc\\1\\2\\3\\4\\5\\6\\7"); Returns the parent directory of the 7 folder C:\abc\1\2\3\4\5\6
Console. Read ();
}
}
}

3, File class files
Copy Code code as follows:

using System;
using System.IO;
using System.Text;
Namespace _13_file {
Class program {
static void Main (string[] args) {
//file static classes need to be aware of the file default encoding when using the files class If the encoding is incorrect, the garbled
file is displayed. Appendalltext ("C:\\1.txt", "gb1232"); Append content to the C:\\1.txt file "gb2312"
//If there is a write file
if. Exists ("C:\\1.txt")) {
File. WriteAllText ("C:\\1.txt", "written in Chinese sometimes garbled) you need to use the third parameter to specify the encoding format of the Encoding file default format", Encoding. Default);//writealltext is a thorough overwrite and Appendalltext is appended
}
//file.readalltext ();//Read file no longer enumerates the following methods to view a document No more examples
//string [] ReadAllLines (string path)//Read text file into string array
//string ReadAllText (string path)/Read text file to string
//writealllines (s Tring path,string[] Contents, the old content is overwritten by saving the string array to the path of the file line by row.
FileInfo fi = new FileInfo ("C:\\2.txt");//instantiated class features are more powerful than file
Fi. AppendText (); It has many methods and properties to view the document itself
Console. Read ();
}
}
}

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.