A. C # file operations provided by
The difference between the file class and the FileInfo class. The main difference between the two is that file cannot be instantiated, only static methods are provided, and the latter can be instantiated, and the method provided is similar to the file class. First look at the next one instance of using FileInfo constructs: FileInfo myfile = new FileInfo ("C:\\abc.txt");
To create a file instance object, now we can look at its properties:
Fileinfo myfile = new fileinfo (" C:\\abc.txt "); Console.WriteLine (myfile. Attributes)//property Console.WriteLine (myfile. CreationTime)//Creation time Console.WriteLine (myfile. CREATIONTIMEUTC);//UTC time Console.WriteLine (myfile. Directory Console.WriteLine (myfile). directoryname);//directory name Console.WriteLine (myfile. Exists);//Whether there is a Console.WriteLine (myfile). Extension); file name extension Console.WriteLine (myfile. FullName);//Full name Console.WriteLine (myfile. IsReadOnly); whether to read only the Console.WriteLine (myfile. LastAccessTime), last accessed Console.WriteLine (myfile. LASTACCESSTIMEUTC); time Console.WriteLine (myfile. LastWriteTime), last read Console.WriteLine (myfile. LASTWRITETIMEUTC); time Console.WriteLine (myfile. Length), and the file Len Console.WriteLine (myfile. Name), file name Console.WriteLine ("Above is the property of the instance file"); |
|
As for the methods provided by file and FileInfo, I do not introduce them all here, we can go to MSDN. See below for an example of a file-related operation.: Read the contents of the file, the middle of some knowledge is not introduced, we can first think about or search for relevant content.
FileInfo myfile = new FileInfo ("C:\\abc.txt"); if (!myfile. Exists)//To determine whether the file exists, MyFile. Create (); FileStream fs = new FileStream ("C:\\abc.txt", FileMode.Open)//Open File StreamReader sr = new StreamReader (fs); input stream String strLine = ""; Try { StrLine = Sr. ReadLine ()//read a row in the file while (strLine!= null)/Not empty { StrLine = Sr. ReadLine (); Continue reading next line } until the last line of the file Sr. Close ();//Off stream } catch (IOException ex) {//Handle exception Throw ex; } |
|
The file class is static, and the file method is static, so the efficiency of using the file method may be higher than using the corresponding FileInfo instance method if you want to perform only one action. All the file methods require the path of the currently-manipulated files. The static method of the File class performs security checks on all methods. If you plan to reuse an object multiple times, consider using the appropriate instance method for FileInfo, because security checks are not always required.
Two. C # 's Directory operations
C # provides DirectoryInfo and directory two classes to handle directory-related operations. The former must be instantiated before it can be used, while the latter only provides static methods. If you use an object more than once, you typically use the former, and the static method provided by the latter is more efficient if you perform only one action
A little higher.
DirectoryInfo constructor: DirectoryInfo di1 = new DirectoryInfo (@ "C:\MyDir");
DirectoryInfo Di2 = new DirectoryInfo ("C:\\MyDir");
The following example shows the name and length of all files in a directory.
DirectoryInfo Info = New DirectoryInfo ("c:\\"); FileInfo []files = Info.getfiles (); foreach (FileInfo text in files) { TextBox1.Text + = Text. Name; TextBox1.Text + = "" + Text. Length; TextBox1.Text + = "\ n"; } |
|
FileInfo []files = Info.getfiles (); The purpose of this method is to get all the files in the current directory, not including subdirectories. Assign all files to a collection of file classes, and then use a foreach loop to get the name and length of each file.
The next section details the reading and writing of the file.