Use Visual C # To clear the recycle bin (1 ),

Source: Internet
Author: User

Use Visual C # To clear the recycle bin (1 ),

The recycle bin of a Windows operating system is a protection measure for files. Its main function is self-evident. In the new Windows 2000 system, when I delete a file to the recycle bin, I press the "Clear recycle bin" button to clear the recycle bin. The prompt "the folder is not empty, "information cannot be deleted. Therefore, you have to recover all the files and then delete the files in the folder to be deleted. Then, you can clear the recycle bin. But this has brought us troubles in the operating system. This article is to use Visual C #'s powerful functions in file processing to make a software, using this software to clear the files to be deleted in the recycle bin, so as to relieve such troubles.

  I. Main Idea of Program Design

We know that the windows system temporarily stores all the file information we want to delete in the "C: Recycled" directory. Use the "Clear recycle bin" button to complete the files to be deleted in this directory. The main idea of this program is based on the above knowledge. The details are as follows:

(1) first, judge whether the "Recycled" directory under the C root directory is empty.

(2). If it is not null, it traverses all file information and deletes the file.

  Ii. Programming and running environment

(1) Windows 2000 Professional Edition

(2). Net FrameWork SDK Beta 2
Iii. Main steps and explanations of Program Design
(1) Determine whether the "Recycled" directory under the C root directory is empty.

To determine whether file information exists in the "C: Recycled" directory, follow these two steps: first, locate the directory. Second, you can determine whether the directory is empty. The following describes the two steps:

I>. How to find the "C: Recycled" directory in the program.

The. Net FrameWork SDK encapsulates a namespace-System. IO. This namespace defines many classes, objects, attributes, and methods related to file and directory processing. Visual C # allows you to flexibly call these resources to perform operations on files and directories. This article mainly uses two types of namespace: Directory class and File class. The former is mainly used in programs to process directory-related operations, such as determining directories and obtaining file information in directories. The latter is mainly used in programs to process file-related operations, such as deleting files. The GetFiles ("Directory name") method in the Directory class is used in the program. This method returns an enumeration type, which is composed of a group of strings. The GetFiles () method forms a string for each file name in the directory, and then consists of these strings to form an enumeration type.

Ii>. Check whether the directory is empty.

This section describes the foreach statement. Visual C # uses this statement to collect enumeration data. In this article, we will use it to list all the information about the deleted files that exist in "C: Recycled.

Int c = 0; // defines this variable to determine whether the file foreach (string s1 in Directory) exists in the Directory. getFiles ("c: \ recycled") // returns the enumerated type {++ c;} if (c> 0) of the string row of the file name) // determine whether a file exists. If c> 0, there is a file in the recycle bin. Otherwise, there is no {..... }


(2) If the "C: Recycled" directory is not empty, it will traverse all the file information in the directory and delete the file.

This section describes the usage of the MessageBox class. People who have used VB and Delphi must know how easy it is to display prompt information in such programming languages. However, in Visual C #, such operations are obviously relatively cumbersome, but the flexibility has been improved. In Visual C #, System. windows. the Forms namespace encapsulates a MessageBox class, which defines some methods (Show methods), objects, and attributes related to the display prompt information. In this program, we will have a detailed introduction to the MessageBox class.

To Delete files on a disk, you must use the Delete () method of the File class in the namespace System. IO. The parameter following the Delete () method is the full path name of the disk where the file is located. To obtain the full path name of the file, you must use the foreach statement, the program uses the foreach statement to obtain the full path name of a file from an enumeration type that contains the full path names of all files. The specific implementation statement is as follows:

If (c> 0) // determines whether the file information in the "C: Recycled" directory exists {DialogResult r = MessageBox. Show ("Are you sure? "," Garbage disposal! ", MessageBoxButtons. YesNo, MessageBoxIcon. Question); // display the" OK "and" cancel "buttons. the icon is displayed as a Question mark. Int ss = (int) r; if (ss = 6) // press the OK button {foreach (string s in Directory. getFiles ("c: \ recycled") // put the full path name in house s {File. delete (s); // Delete this file }}}

Iii. Main steps and explanations of Program Design
(1) Determine whether the "Recycled" directory under the C root directory is empty.

To determine whether file information exists in the "C: Recycled" directory, follow these two steps: first, locate the directory. Second, you can determine whether the directory is empty. The following describes the two steps:

I>. How to find the "C: Recycled" directory in the program.

The. Net FrameWork SDK encapsulates a namespace-System. IO. This namespace defines many classes, objects, attributes, and methods related to file and directory processing. Visual C # allows you to flexibly call these resources to perform operations on files and directories. This article mainly uses two types of namespace: Directory class and File class. The former is mainly used in programs to process directory-related operations, such as determining directories and obtaining file information in directories. The latter is mainly used in programs to process file-related operations, such as deleting files. The GetFiles ("Directory name") method in the Directory class is used in the program. This method returns an enumeration type, which is composed of a group of strings. The GetFiles () method forms a string for each file name in the directory, and then consists of these strings to form an enumeration type.

Ii>. Check whether the directory is empty.

This section describes the foreach statement. Visual C # uses this statement to collect enumeration data. In this article, we will use it to list all the information about the deleted files that exist in "C: Recycled.

Int c = 0; // defines this variable to determine whether the file foreach (string s1 in Directory) exists in the Directory. getFiles ("c: \ recycled") // returns the enumerated type {++ c;} if (c> 0) of the string row of the file name) // determine whether a file exists. If c> 0, there is a file in the recycle bin. Otherwise, there is no {..... }


(2) If the "C: Recycled" directory is not empty, it will traverse all the file information in the directory and delete the file.

This section describes the usage of the MessageBox class. People who have used VB and Delphi must know how easy it is to display prompt information in such programming languages. However, in Visual C #, such operations are obviously relatively cumbersome, but the flexibility has been improved. In Visual C #, System. windows. the Forms namespace encapsulates a MessageBox class, which defines some methods (Show methods), objects, and attributes related to the display prompt information. In this program, we will have a detailed introduction to the MessageBox class.

To Delete files on a disk, you must use the Delete () method of the File class in the namespace System. IO. The parameter following the Delete () method is the full path name of the disk where the file is located. To obtain the full path name of the file, you must use the foreach statement, the program uses the foreach statement to obtain the full path name of a file from an enumeration type that contains the full path names of all files. The specific implementation statement is as follows:

If (c> 0) // determines whether the file information in the "C: Recycled" directory exists {DialogResult r = MessageBox. Show ("Are you sure? "," Garbage disposal! ", MessageBoxButtons. YesNo, MessageBoxIcon. Question); // display the" OK "and" cancel "buttons. the icon is displayed as a Question mark. Int ss = (int) r; if (ss = 6) // press the OK button {foreach (string s in Directory. getFiles ("c: \ recycled") // put the full path name in house s {File. delete (s); // Delete this file }}}

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.