ASP. NET design network hard disk two important classes of code

  • 2020-05-17 05:17:20
  • OfStack

System.IO.File class and System.IO.FileInfo class

In the process of designing and implementing a "network hard disk", a lot of content related to file system operation will be used. This section begins with a brief introduction to the two.NET classes associated with the file system.

The System.IO.File class and the System.IO.FileInfo class mainly provide various operations related to files, requiring reference to the System.IO namespace when used. The main properties and methods are described below through a program example.

(1) file opening method: File.Open

The method is declared as follows:

public static FileStream Open(string path,FileMode mode)

The following code opens the newFile.txt file in the c:\tempuploads directory and writes hello to it.
 
private void OpenFile() 
{ 
 FileStream.TextFile=File.Open(@"c:\tempuploads\newFile.txt",FileMode.Append); 
 byte [] Info = {(byte)'h',(byte)'e',(byte)'l',(byte)'l',(byte)'o'}; 
 TextFile.Write(Info,0,Info.Length); 
 TextFile.Close(); 
} 

(2) file creation method: File.Create

The method is declared as follows:

public static FileStream Create(string path;)

The following code shows how to create a file named newFile.txt under c:\tempuploads.

Because the File.Create method grants all users full read/write access to a new file by default, the file is opened with read/write access and must be closed before it can be opened by another application. To do this, you need to close the file you created using the Close method of the FileStream class.
 
private void MakeFile() 
{ 
 FileStream NewText=File.Create(@"c:\tempuploads\newFile.txt"); 
 NewText.Close(); 
} 

(3) file deletion method: File.Delete

The method is declared as follows:

public static void Delete(string path);

The following code shows how to delete the newFile.txt file in the c:\tempuploads directory.
 
private void DeleteFile() 
{ 
 File.Delete(@"c:\tempuploads\newFile.txt"); 
} 

(4) file copy method: File.Copy

The method is declared as follows:

public static void Copy(string sourceFileName,string destFileName,bool overwrite);

The following code copies c:\tempuploads\ newFile.txt to c:\tempuploads\ BackUp.txt.

Since the OverWrite parameter for the Cope method is set to true, the BackUp.txt file will be overwritten by the copied file if it already exists.
 
private void CopyFile() 
{ 
 File.Copy(@"c:\tempuploads\newFile.txt",@"c:\tempuploads\BackUp.txt",true); 
} 

(5) file moving method: File.Move

The method is declared as follows:

public static void Move(string sourceFileName,string destFileName);

The following code can move the BackUp.txt file under c:\tempuploads to the c root directory.

Note:

File transfer can only be done under the same logical disk. If you try to transfer files under c disk to d disk, an error will occur.
 
private void MoveFile() 
{ 
 File.Move(@"c:\tempuploads\BackUp.txt",@"c:\BackUp.txt"); 
} 

(6) setting file property method: File.SetAttributes

The method is declared as follows:

public static void SetAttributes(string path,FileAttributes fileAttributes);

The following code can set the property of c:\tempuploads\ newFile.txt to read only, hide.
 
private void SetFile() 
{ 
 File.SetAttributes(@"c:\tempuploads\newFile.txt", 
 FileAttributes.ReadOnly|FileAttributes.Hidden); 
} 

In addition to the commonly used read-only and hidden properties, there are also Archive(file archive state), System(system file), Temporary(temporary file), and so on. See FileAttributes's description in MSDN for details on file properties.

(7) the method to determine whether a document exists: File.Exist

The method is declared as follows:

public static bool Exists(string path);

The following code determines whether the c:\tempuploads\ newFile.txt file exists. If it exists, first copy the file, then delete it, and finally move the copied file; If it does not exist, create the file, open the file, write to it, and set the file properties to read-only, hidden.
 
if(File.Exists(@"c:\tempuploads\newFile.txt")) // Determine if the file exists  
{ 
 CopyFile(); // Copy the file  
 DeleteFile(); // Delete the file  
 MoveFile(); // Move files  
} 
else 
{ 
 MakeFile(); // Generate the file  
 OpenFile(); // Open the file  
 SetFile(); // Set file properties  
} 

In addition, the File class provides more support for the Text text.

· AppendText: appends text to existing files

· CreateText: creates or opens a new file for writing text

· OpenText: open the existing text file for reading

However, the above method mainly operates on the encoded text of UTF-8, so it is not flexible enough. It is recommended that you use the code below to manipulate the txt file.

· "read" the txt file. The sample code is as follows:
 
StreamReader TxtReader = new StreamReader(@"c:\tempuploads\newFile.txt",System.Text.Encoding.Default); 
string FileContent; 
FileContent = TxtReader.ReadEnd(); 
TxtReader.Close(); 

· "write" to the txt file. The sample code is as follows:
 
StreamWriter = new StreamWrite(@"c:\tempuploads\newFile.txt",System.Text.Encoding.Default); 
string FileContent; 
TxtWriter.Write(FileContent); 
TxtWriter.Close(); 


System.IO.Directory class and System.DirectoryInfo class

It mainly provides a variety of operations about the directory, so you need to refer to the System.IO namespace when using it. The main properties and methods are described below through a program example.

(1) directory creation method: Directory.CreateDirectory

The method is declared as follows:

public static DirectoryInfo CreateDirectory(string path);

The following code demonstrates creating a directory named NewDirectory under the c: tempuploads folder.
 
private void MakeDirectory() 
{ 
 Directory.CreateDirectory(@"c:\tempuploads\NewDirectoty"); 
} 

(2) directory property setting method: DirectoryInfo.Atttributes

The following code sets the c:\tempuploads\NewDirectory directory to read only, hidden. Like the file properties, the directory properties are set using FileAttributes.
 
private void SetDirectory() 
{ 
 DirectoryInfo NewDirInfo = new DirectoryInfo(@"c:\tempuploads\NewDirectoty"); 
 NewDirInfo.Atttributes = FileAttributes.ReadOnly|FileAttributes.Hidden; 
} 

(3) directory deletion method: Directory.Delete

The method is declared as follows:

public static void Delete(string path,bool recursive);

The following code can delete the c: tempuploads\BackUp directory. The second parameter of the Delete method is of type bool, which determines whether to delete non-empty directories. If the parameter value is true, the entire directory will be deleted, even if there are files or subdirectories in the directory. If it is false, it can be deleted only if the directory is empty.
 
private void DeleteDirectory() 
{ 
 Directory.Delete(@"c:\tempuploads\BackUp",true); 
} 

(4) directory moving method: Directory.Move

The method is declared as follows:

public static void Move(string sourceDirName,string destDirName);

The following code moves the directory c:\tempuploads\NewDirectory to c:\tempuploads\BackUp.
 
private void MoveDirectory() 
{ 
 File.Move(@"c:\tempuploads\NewDirectory",@"c:\tempuploads\BackUp"); 
} 

(5) get all subdirectory methods under the current directory: Directory.GetDirectories

The method is declared as follows:

public static string[] GetDirectories(string path;);

The following code reads out all the subdirectories in the c: tempuploads\ directory and stores them in an array of strings.
 
private void GetDirectory() 
{ 
 string [] Directorys; 
 Directorys = Directory. GetDirectories (@"c:\tempuploads"); 
} 

(6) get all the files in the current directory: Directory.GetFiles

The method is declared as follows:

public static string[] GetFiles(string path;);

The following code reads out all the files in the c: tempuploads\ directory and stores them in an array of strings.
 
private void GetFile() 
{ 
 string [] Files; 
 Files = Directory. GetFiles (@"c:\tempuploads",); 
} 

(7) determine whether the directory exists: Directory.Exist

The method is declared as follows:
 
public static bool Exists( 
 string path; 
); 

The following code determines if the c:\tempuploads\NewDirectory directory exists. If it exists, get the subdirectories and files in the directory first, then move them, and finally delete the moved directory. If it does not exist, the directory is created and the directory property is made read-only and hidden.
 
if(File.Exists(@"c:\tempuploads\NewDirectory")) // Determine if the directory exists  
{ 
 GetDirectory(); // Get subdirectories  
 GetFile(); // Access to the file  
 MoveDirectory(); // Mobile directory  
 DeleteDirectory(); // Delete the directory  
} 
else 
{ 
 MakeDirectory(); // Generated directory  
 SetDirectory(); // Set directory properties  
} 

Note:

There are three ways of path, the relative path under the current directory, the relative path of the current working disk, and the absolute path. Take the example of C:\Tmp\Book (assuming the current working directory is C:\Tmp). "Book", "\Tmp\Book", "C:\Tmp\Book" all represent C:\Tmp\Book.

In addition, "\" is a special character in C#, so you need to use "\\" to indicate it. Because of the inconvenience of writing this way, the C# language provides @ to simplify it. You can use "\" directly by adding @ to the string. So the path above should be represented in C# as "Book", @ "\Tmp\Book", @ "C:\Tmp\Book".

Related articles: