Two important types of network hard disk development in ASP. NET

Source: Internet
Author: User

To design the "Network hard disk" function, you must first familiarize yourself with the operations for processing files and folders in. NET. File and Directory are the two most important classes. Understanding them will greatly facilitate the implementation of subsequent functions.

System. IO. File and System. IO. FileInfo

In the process of designing and implementing a "Network hard disk", a large amount of content related to file system operations will be used. This section briefly introduces the two. NET classes related to the file system.

The System. IO. File class and System. IO. FileInfo class provide various operations on files. The System. IO namespace must be referenced during use. The following describes the main attributes and methods of a program instance.

(1) File opening method: File. Open

The statement of this method is as follows:

Public static FileStream Open (string path, FileMode mode)
The following code opens the file named newfile.txt in the c: \ tempuploadsdirectory 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 statement 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.

Because File. by default, the Create method grants all users full read/write access to the new file. Therefore, the file is opened with the read/write access permission and must be disabled before IT can be used! # Open the application. Therefore, you must use the Close method of the FileStream class to Close the created file.

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 deletes the newfile.txt file in the c: \ tempuploadsdirectory.

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.

The overwritegion of copetypes is set to true, so that if the backup.txt file already exists, it will be overwritten by the copied file.

Private void CopyFile ()
{
File. Copy (@ "c: \ tempuploads \ newFile.txt", @ "c: \ tempuploads \ BackUp.txt", true );
}
(5) File movement method: File. Move

The method is declared as follows:

Public static void Move (string sourceFileName, string destFileName );
The following code moves the backup.txt file under c: \ tempuploadsto the c root directory.

Note:

File Transfer can only be performed on the same logical disk. If you try to transfer files from drive C to drive D, an error will occur.

Private void MoveFile ()
{
File. Move (@ "c: \ tempuploads \ BackUp.txt", @ "c: \ BackUp.txt ");
}
(6) File Attribute setting method: File. SetAttributes

The method is declared as follows:

Public static void SetAttributes (string path, FileAttributes fileAttributes );
The following code sets the properties of file c: \ tempuploads \ newFile.txt to read-only and hidden.

Private void SetFile ()
{
File. SetAttributes (@ "c: \ tempuploads \ newFile.txt ",
FileAttributes. ReadOnly | FileAttributes. Hidden );
}
In addition to common read-only and hidden attributes, files include Archive, System, and Temporary. For details about file attributes, see the description of FileAttributes in MSDN.

(7) method for determining whether a File exists: File. Exist

The method is declared as follows:

Public static bool Exists (string path );

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next Page

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.