The c file action example is fully commented

  • 2020-06-01 10:52:53
  • OfStack


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
namespace Example 
{ 

    class Program 
    { 

        static void Main(string[] args) 
        { 
            ////////////////    File open    The following code opens D:\wang.txt File, and write to the file "hello" 
            FileStream textFile = File.Open(@"D:\wang.txt", FileMode.Append);// In order to Append Mode to open the file (if it does not exist, it will be created)  
            byte[] info = { (byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o' };// The information to write  
            textFile.Write(info, 0, info.Length);//Write Method can only write byte An array of                         
            textFile.Close();// Close file stream  
           
            //////////////////////  File creation     
            FileStream newText = File.Create(@"D:\newText.txt");// Create a file  
            newText.Close();// Close the file  
            ////////////////////   Delete the file  
            File.Delete(@"d:\newText.txt"); 
            //////////////////   Copying files     If the target file exists, replication is not allowed ) 
            //File.Copy(@"d:\wang.txt", @"d:\CopyWang.txt"); 

            ////////////////   Move files    Only in the same 1 Intraday movement    If the target path is incorrect, it cannot be moved  
           // File.Move(@"d:\CopyWang.txt", @"D:\A\movewang.txt"); 
            ////////////////////////   Set the file property to   Read only, hide  
            //File.SetAttributes(@"D:\copywang.txt", FileAttributes.ReadOnly | FileAttributes.Hidden);// Satisfy more than one property at a time, must use bit or (|). 
            ///////////////   Determine if the file exists  
            if (File.Exists(@"D:\copywang.txt"))// If there is    Even hidden files can be found  
            { 
                File.SetAttributes(@"D:\copywang.txt", FileAttributes.ReadOnly);// After resetting the properties, the hidden files will also be displayed, as long as they are not added Hidden attribute  
                Console.WriteLine(" Find the file copywang.txt"); 
            } 
            else 
            { 
                Console.WriteLine(" No file found CopyWang.txt"); 
            } 
            /* 
             In addition, File Class for Text The text provides more support.  
          ?AppendText : appends text to an existing file  
          ?CreateText : creates or opens a new file for writing text  
          ?OpenText : opens an existing text file for reading  
           But the above method is mainly correct UTF-8 Is not flexible enough to manipulate the encoded text. The following code pairs are recommended for the reader here txt The file is manipulated.  
          ? right txt File to "read" operation, sample code as follows:    
             */ 
            StreamReader textReader = new StreamReader(@"D:\wang.txt", System.Text.Encoding.Default);// Open the file with the default encoding  
            string str = textReader.ReadToEnd();// Read the file  
            Console.WriteLine(" use StreamReader Read the text content :" + str); 
            textReader.Close(); 
            ////////////////// right txt File write content  
            StreamWriter textWriter = new StreamWriter(@"D:\wang.txt"); 
            str = "Learn .Net"; 
            textWriter.Write(str); 
            textWriter.Close(); 
            /* 
            System.IO.Directory Classes and System.DirectoryInfo class  
            It mainly provides various operations about the directory, which need to be referenced when used System.IO Namespace. The main properties and methods are described below through a program example.  
            */ 
            Directory.CreateDirectory(@"D:\wang1\wang");// Create a directory (folder) if it already exists, keep it ; You can also 1 Create multiple levels of directories  
            ///////////////////////////////// Directory property setting method  
            DirectoryInfo dirInfo = new DirectoryInfo(@"D:\wang1\wang");// 
            dirInfo.Attributes = FileAttributes.Hidden;// | FileAttributes.ReadOnly;// Set folder properties  
            /////////////////Delete Methods the first 2 The parameters for bool Type, which can decide whether to delete a non-empty directory.  
            // If the parameter value is true , will delete the entire directory, even if there are files or subdirectories under the directory; if false , can be deleted only if the directory is empty.  
            //Directory.Delete(@"D:\wang1", true);// If the file is set to ReadOnly, You can't delete it  
            //Directory.Move(@"d:\wang1", @"d:\wang3");// The folder wang1 Move to folder wang3 In the , Is equivalent to wang1 Delete, create 1 a wang3 And then move the content to wang3 
            string[] Directories = Directory.GetDirectories(@"D:\wang3");// Get folder wang3 The directory where the  
            foreach (string var in Directories) 
                Console.WriteLine(var); 
            string[] Files = Directory.GetFiles(@"D:\wang1");// Get folder wang1 All the files below  
            foreach (string var in Files) 
                Console.WriteLine(var); 
            if (Directory.Exists(@"D:\wang1")) 
                Console.WriteLine(" folder wang1 There are "); 
            /* 
             in C# In the   " \ "Is a special character that needs to be used to represent it." \\ ". Because it's not a convenient way to write it, C# Language provides @ Let's simplify it. Just put it in front of the string @ Can be used directly." \ ".  
                 So the path up here is C# Where should be expressed as" Book ", @ " \Tmp\Book ", @ " C:\Tmp\Book ".  
            */ 
            Console.ReadLine();   
        } 
    } 
}


Related articles: