To design the "network hard disk" function, you must first be familiar with the operations of processing files and folders in .NET. File class and Directory class are the two most important classes. Understanding them will greatly facilitate the implementation of subsequent functions.
The System.IO.File class and the System.IO.FileInfo class
will make extensive use of content related to file system operations in the process of designing and implementing the "network hard disk". Therefore, this section first briefly introduces the two .NET classes related to the file system.
The System.IO.File class and the System.IO.FileInfo class mainly provide various operations related to files, and you need to reference the System.IO namespace when using them. Its main attributes and methods are introduced below through program examples.
(1) File opening method: File.Open
The declaration of this method is as follows:
public static FileStream Open(string path,FileMode mode)
The following code opens the file named newFile.txt stored in the c:tempuploads directory, and in the file Write hello in .
private void OpenFile()
{
FileStream.TextFile=File.Open(@"c:tempuploadsnewFile.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 declaration of this method is as follows:
public static FileStream Create(string path;)
The following code demonstrates how to create a file named newFile.txt under c:tempuploads.
Since the File.Create method grants full read/write access to new files by default to all users, 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 use the Close method of the FileStream class to close the created file.
private void MakeFile()
{
FileStream NewText=File.Create(@"c:tempuploadsnewFile.txt");
NewText.Close();
}
(3) File deletion method: File.Delete
This method is declared as follows:
public static void Delete(string path);
The following code demonstrates how to delete the newFile.txt file in the c:tempuploads directory.
private void DeleteFile()
{
File.Delete(@"c:tempuploadsnewFile.txt");
}
(4) File copy method: File.Copy
This method is declared as follows:
public static void Copy(string sourceFileName,string destFileName,bool overwrite);
The following code copies c:tempuploadsnewFile.txt to c:tempuploads BackUp.txt.
Since the OverWrite parameter of the Cope method is set to true, if the BackUp.txt file already exists, it will be overwritten by the copied file.
private void CopyFile()
{
File.Copy(@"c:tempuploadsnewFile.txt",@"c:tempuploadsBackUp.txt",true);
}
(5) File move method: File.Move
This 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 root directory of drive c .
Note:
File transfer can only be done under the same logical disk. If you try to transfer files from the c drive to the d drive, an error will occur.
private void MoveFile()
{
File.Move(@"c:tempuploadsBackUp.txt",@"c:BackUp.txt");
}
(6) Set file attributes method: File.SetAttributes
This method is declared as follows:
public static void SetAttributes(string path,FileAttributes fileAttributes);
The following code can set the attributes of the file c:tempuploadsnewFile.txt to read-only or hidden .
private void SetFile()
{
File.SetAttributes(@"c:tempuploadsnewFile.txt",
FileAttributes.ReadOnly|FileAttributes.Hidden);
}
In addition to the commonly used read-only and hidden attributes, files also have Archive (file archive status), System (system files), Temporary (temporary files), etc. For details about file attributes, please refer to the description of FileAttributes in MSDN.
(7) Method to determine whether a file exists: File.Exist
This method is declared as follows:
public static bool Exists(string path);
The following code determines whether the c:tempuploadsnewFile.txt file exists. If it exists, first copy the file, then delete it, and finally move the copied file; if it does not exist, first create the file, then open the file and perform writing operations, and finally set the file attributes to read-only and hidden.
if(File.Exists(@"c:tempuploadsnewFile.txt")) //Determine whether the file exists
{
CopyFile(); //Copy the fileDeleteFile(); //Delete the fileMoveFile(); //Move the file
}
else
{
MakeFile(); //Generate file OpenFile(); //Open file SetFile(); //Set file attributes
}
In addition, the File class provides more support for Text text.
· AppendText: Append text to an existing file
· CreateText: Create or open a new file for writing text
· OpenText: Open an existing text file for reading
But the above method mainly operates on UTF-8 encoded text, thus appearing Not flexible enough. It is recommended that readers use the following code to operate txt files.
· Perform "read" operation on txt files. The sample code is as follows:
StreamReader TxtReader = new StreamReader(@"c:tempuploadsnewFile.txt",System.Text.Encoding.Default);
string FileContent;
FileContent = TxtReader.ReadEnd();
TxtReader.Close();
· Perform "write" operation on txt file. The sample code is as follows:
StreamWriter = new StreamWrite(@"c:tempuploadsnewFile.txt",System.Text.Encoding.Default);
string FileContent;
TxtWriter.Write(FileContent);
TxtWriter.Close();
The System.IO.Directory class and the System.DirectoryInfo class
mainly provide various operations on the directory, and you need to reference the System.IO namespace when using them. Its main attributes and methods are introduced below through program examples.
(1) Directory creation method: Directory.CreateDirectory
This method is declared as follows:
public static DirectoryInfo CreateDirectory(string path);
The following code demonstrates creating a directory named NewDirectory in the c:tempuploads folder.
private void MakeDirectory()
{
Directory.CreateDirectory(@"c:tempuploadsNewDirectoty");
}
(2) Directory attribute setting method:
The code below DirectoryInfo.Atttributes sets the c:tempuploadsNewDirectory directory to read-only and hidden. Like file attributes, directory attributes are also set using FileAttributes.
private void SetDirectory()
{
DirectoryInfo NewDirInfo = new DirectoryInfo(@"c:tempuploadsNewDirectoty");
NewDirInfo.Atttributes = FileAttributes.ReadOnly|FileAttributes.Hidden;
}
(3) Directory deletion method: Directory.Delete
This method is declared as follows:
public static void Delete(string path,bool recursive);
The following code can delete the c:tempuploadsBackUp directory. The second parameter of the Delete method is of type bool, which can determine whether to delete a non-empty directory. 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, the directory will be deleted only if it is empty.
private void DeleteDirectory()
{
Directory.Delete(@"c:tempuploadsBackUp",true);
}
(4) Directory move method: Directory.Move
This method is declared as follows:
public static void Move(string sourceDirName, string destDirName);
The following code moves the directory c:tempuploadsNewDirectory to c:tempuploadsBackUp.
private void MoveDirectory()
{
File.Move(@"c:tempuploadsNewDirectory",@"c:tempuploadsBackUp");
}
(5) Get all subdirectories in the current directory: Directory.GetDirectories
This method is declared as follows:
public static string[] GetDirectories(string path;);
The following code reads out all subdirectories in the c:tempuploads directory , and store it into a string array.
private void GetDirectory()
{
string [] Directorys;
Directorys = Directory. GetDirectories (@"c:tempuploads");
}
(6) Method to get all files in the current directory: Directory.GetFiles
This 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 Store it into a string array.
private void GetFile()
{
string [] Files;
Files = Directory. GetFiles (@"c:tempuploads",);
}
(7) Method to determine whether a directory exists: Directory.Exist.
This method is declared as follows:
public static bool Exists(
string path;
);
The following code determines whether the c:tempuploadsNewDirectory directory exists. If it exists, first obtain the subdirectories and files in the directory, then move them, and finally delete the moved directory. If it does not exist, create the directory first, and then set the directory attributes to read-only and hidden.
if(File.Exists(@"c:tempuploadsNewDirectory")) //Determine whether the directory exists
{
GetDirectory(); //Get the subdirectory GetFile(); //Get the file MoveDirectory(); //Move the directory DeleteDirectory(); //Delete the directory
}
else
{
MakeDirectory(); //Generate directory SetDirectory(); //Set directory attributes
}
Note:
There are three ways to specify a path, the relative path in the current directory, the relative path in the current working disk, and the absolute path. Take C:TmpBook as an example (assuming the current working directory is C:Tmp). "Book", "TmpBook", "C:TmpBook" all mean C:TmpBook.
In addition, "" is a special character in C#, and you need to use "\" to express it. Since this way of writing is inconvenient, the C# language provides @ to simplify it. Just add @ in front of the string to use "" directly. So the above path should be expressed as "Book", @"TmpBook", @"C:TmpBook" in C#.