The Path class and Directory class and File class operate on the and directory and file

  • 2020-05-27 04:43:17
  • OfStack

Path operates on the path string
Get the suffix
Can merge paths
Get file name

Directory and DirectoryInfo operate on the directory
Determine if the directory exists
Create a directory
Delete the directory
Gets all the subdirectories under the directory
Gets all the subfiles in the directory

File and FileInfo operate on files
Read the file
Write files
Additional documents
Determine if the file exists
Create a file
Delete the file

1, Path class
 
using System; 
using System.IO;// The namespace for directory and file operations  
namespace _11_Path class  { 
class Program { 
static void Main(string[] args) { 
string path = "c:\\abc\\1.txt" ; 
// Notice that this is an operation on a path string   Not the actual document   "Modify" supports the string level, without actually renaming the file  
path = Path.ChangeExtension(path, "avi" );//ChangeExtension() Modify the file suffix name 1.avi c:\\abc\\1.avi 
// Combine the two paths 1 Two paths + Ok, it's easy to solve the problem of whether or not to add slashes, and automatically handle the problem of path separators  
path = Path.Combine("c:\\abc\\def\\" , "1.jpg"); //c:\abc\def\1.jpg 
// Get the location of the file in the folder   Again, from the point of view of strings  
path = Path.GetDirectoryName(path);//c:\abc 
path = Path.GetExtension(path);// extension  .txt 
path = Path.GetFileName(path);// The file name . suffix  1.txt 
path = Path.GetFileNameWithoutExtension(path);// File name without suffix  1 
path = Path.GetFullPath("11-Path class .exe" );// File full path ( The full path of the relative file  1 This method is usually not used ) F:\PIZIYIMAO\11-Path class \bin\Debug\11-Path class .exe 
path = Path.GetTempFileName();// The temporary folder saves the path   Automatic file creation  C:\Documents and Settings\PIZIYIMAO\Local Settings\Temp\tmp5E.tmp 
path = Path.GetTempPath();// Gets the temporary folder save path  C:\Documents and Settings\PIZIYIMAO\Local Settings\Temp\ 
Console.WriteLine(path); 
Console.Read(); 
} 
} 
} 

2. Operate the directory classes Directory and DirectoryInfo
 
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; // Gets the full path to the file   Function than Directory strong   The difference is that it is an instance class   The latter is a static class  
Directory .CreateDirectory("c:\\abc" ); // Create folders  
Directory .CreateDirectory("c:\\abc\\1\\2\\3\\4\\5\\6\\7" ); // Continuously create multiple levels of folders  
if (Directory .Exists( "c:\\abc")) // Determine if a folder exists  
{ 
Directory .Delete("c:\\abc" ); // Delete if it exists   If the folder is empty, it can be deleted normally   If it is not empty, it will report an error  " The directory is not empty " 
Directory .Delete("c:\\abc" , true); //true Specifies the   If the folder is not empty   Perform the same delete operation  
} 
string [] paths = Directory .GetDirectories( "c:\\abc"); // Gets the names of all subdirectories in the directory   Just take it off 1 level   namely c:\abc\1  Such as access to windows This method can be used for all folder paths under folders  
string [] paths2 = Directory .GetDirectories( "c:\\windows", "$*" );// The above method overload implementation   Searching for $ Opening file  
string [] paths3 = Directory .GetDirectories( "c:\\abc", "*" , SearchOption .AllDirectories);// Wildcards look for qualified files in folders   Includes subfolders  
foreach (string path in paths) { 
Console .WriteLine(path); 
} 
string [] files = Directory .GetFiles( "c:\\windows"); // Walk through all the files in the folder  
string [] files2 = Directory .GetFiles( "c:\\windows", "*.ini" , SearchOption .AllDirectories);// Wildcards look up files in directories   Usage is similar to GetDirectories 
foreach (string file in files2) { 
Console .WriteLine(file); 
} 
// The most important thing about directory operations is that  GetFiles and GetDirectories methods  
Directory .GetParent("c:\\abc\\1\\2\\3\\4\\5\\6\\7" ); // return 7 The parent directory of the folder  c:\abc\1\2\3\4\5\6 
Console .Read(); 
} 
} 
} 

3. File File
 
using System; 
using System.IO; 
using System.Text; 
namespace _13_File { 
class Program { 
static void Main( string[] args) { 
//file Static class   use file Class to be aware of the use of file default encoding   If the encoding is incorrect   Garbled code will appear in the file  
File .AppendAllText("c:\\1.txt" , "gb1232"); // to c:\\1.txt Append to file   Content" gb2312 "  
// If there is a write file  
if (File .Exists( "c:\\1.txt")) { 
File .WriteAllText("c:\\1.txt" , " Writing in Chinese can sometimes appear garbled   Need to use regulation 3 A parameter   The specified Encoding The encoding format of the file  Default Is the default format " ,Encoding .Default);//WriteAllText It's complete coverage   while AppendAllText Is additional  
} 
//File.ReadAllText();// Read files are no longer enumerated   The following method of viewing a document is no longer an example  
//string[] ReadAllLines(string path) // Reads the text file into an array of strings  
//string ReadAllText(string path) // Reads the text file into the string  
//WriteAllLines(string path,string[] contents) . // Saves the array of strings line by line to a file path In, the old content will be overwritten.  
FileInfo fi = new FileInfo( "c:\\2.txt" );// Instantiated class   Function than file More powerful  
fi.AppendText(); // It has many ways   And attributes   Their view   The document  
Console .Read(); 
} 
} 
} 

Related articles: