Asp. NET design two important classes of network hard disk

Source: Internet
Author: User
Tags array bool file copy file system relative backup
asp.net| Design | network | hard disk

To do the "network hard disk" function design, first of all to be familiar with. NET to manipulate files and folders. The file class and directory class are the two most important classes. Understanding them will provide a great convenience for the implementation of later functionality.

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

In the design and implementation of "Network hard disk" process, will be a lot of use and file system operations related content. Therefore, this section first on the file system-related two. NET class for a brief introduction.

The System.IO.File class and the System.IO.FileInfo class provide a variety of operations about the file and need to reference the System.IO namespace when used. The following is an example of a program that describes its main properties and methods.

(1) File Open method: File.Open

The declaration of the method is as follows:

public static FileStream Open (string path,filemode mode)
The following code opens the file stored in the C:\tempuploads directory with the name NewFile.txt and writes hello to the file.

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 declaration of the method is 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 full read/write access to new files to all users by default, the file is opened with read/write access and must be closed before it can be opened by another application. To do so, you need to use the Close method of the FileStream class to shut down the file you created.

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.

Because the overwrite parameter of the Cope method is set to True, if the BackUp.txt file already exists, it will be overwritten by the file that was copied past.

private void CopyFile ()
{
File.Copy (@ "C:\tempuploads\newFile.txt", @ "C:\tempuploads\BackUp.txt", true);
}
(5) file Move 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-packing directory.

Attention:

File transfers can only be performed under the same logical disk. An error occurs if you attempt to transfer files under C disk to D disk.

private void MoveFile ()
{
File.move (@ "C:\tempuploads\BackUp.txt", @ "C:\BackUp.txt");
}
(6) Set 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 properties of the file C:\tempuploads\newFile.txt to read-only, hidden.

private void Setfile ()
{
File.setattributes (@ "C:\tempuploads\newFile.txt",
fileattributes.readonly| Fileattributes.hidden);
}
In addition to the commonly used read-only and hidden attributes, there are archive (file archive status), System files, temporary (temporary files), and so on. For more information on file properties, refer to the FileAttributes in MSDN.

(7) methods to determine whether a file exists: File.exist

The method is declared as follows:

public static bool Exists (string path);
The following code determines whether a c:\tempuploads\newFile.txt file exists. If it exists, copy the file, delete it, and then move the copied file, if it does not exist, create the file first, open the file and write, and finally set the file property to read-only and hide.

if (File.exists (@ "C:\tempuploads\newFile.txt"))//Determine if the file exists
{
CopyFile (); Copying files
DeleteFile (); deleting files
MoveFile (); Moving files
}
Else
{
MakeFile (); Generating files
OpenFile (); Open File
Setfile (); Set file properties
}
In addition, the file class provides more support for text literals.

· 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 carries on the operation to the UTF-8 encoded text, thus appears insufficiently nimble. Here the reader is recommended to use the following code to operate the TXT file.

· For TXT file "read" operation, 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 TXT file, sample code 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 on the directory, which need to refer to the System.IO namespace. The following is an example of a program that describes its main properties and methods.

(1) Directory creation method: Directory.CreateDirectory

The method is declared as follows:

public static DirectoryInfo CreateDirectory (string path);
The following code shows the creation of 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. As with file properties, 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 type bool, and it can decide whether to delete the Non-empty directory. True to delete the entire directory, even if there are files or subdirectories under that directory, or false to delete only if the directory is empty.

private void DeleteDirectory ()
{
Directory.delete (@ "C:\tempuploads\BackUp", true);
}
(4) directory Move 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 subdirectories in the current directory method: Directory.getdirectories

The method is declared as follows:

public static string[] GetDirectories (string path;);
The following code reads all subdirectories under the C:\tempuploads\ directory and stores them in a string array.

private void Getdirectory ()
{
string [] Directorys;
Directorys = Directory. GetDirectories (@ "c:\tempuploads");
}
(6) Get all the file methods under the current directory: Directory.GetFiles

The method is declared as follows:

public static string[] GetFiles (string path;);
The following code reads all the files in the C:\tempuploads\ directory and stores them in a string array.

private void GetFile ()
{
string [] Files;
Files = Directory. GetFiles (@ "C:\tempuploads",);
}
(7) Determine the existence of a directory method: Directory.exist

The method is declared as follows:

public static bool Exists (
string path;
);
The following code determines whether the C:\tempuploads\NewDirectory directory exists. If so, get the subdirectories and files in the directory, then move them, and then delete the directory after the move. If it does not exist, create the directory first, and then set the Directory property to read-only, hidden.

if (File.exists (@ "c:\tempuploads\NewDirectory"))//Determine if the directory exists
{
Getdirectory (); Get subdirectories
GetFile (); Get files
MoveDirectory (); Move Directory
DeleteDirectory (); Delete Directory
}
Else
{
Makedirectory (); Build Directory
Setdirectory (); Setting Directory Properties
}
Attention:

There are 3 ways to path, the relative path under the current directory, the relative path of the current work disk, and the absolute path. Take C:\Tmp\Book For example (assuming the current working directory is C:\TMP). "Book", "\tmp\book", "C:\Tmp\Book" all represent C:\Tmp\Book.

In addition, in C # "\" is a special character, you need to use "\" to indicate it. Because this is not convenient, the C # language provides the @ to simplify it. You can use "\" just by adding @ before the string. So the path above should be expressed as "book" in C #, @ "\tmp\book", @ "C:\Tmp\Book".



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.