To empty the Recycle Bin with Visual C #

Source: Internet
Author: User
Tags foreach command line contains empty execution
Visual C #, the next-generation mainstream programming language, is well known for its powerful language capabilities. And there is a very rich content of the program development package--. Net FrameWork SDK, as his backing. This makes Visual C # a stage to give full play to its capabilities. The powerful features of Visual C # are not only in supporting data processing of various databases, but also in the aspects of network programming and file processing.

Windows operating system of the Recycle Bin is a document protection measures, he mainly is the role is self-evident. In the new Windows 2000 system, when I delete the file to the Recycle Bin, press the "Empty Recycle Bin" button, want to empty the Recycle Bin, at this point, often prompted "So-and-so folder is not empty, can not delete" information. So have to restore all, and then to delete the folder, first delete the file inside, and then delete the folder, so you can empty the Recycle Bin. But this has brought trouble to our operating system. This article is to take advantage of Visual C # in the processing of files, a powerful function to do a software, using this software to empty the Recycle Bin inside the file to delete, thereby relieving this annoyance.

   I. Main ideas of programming

We know that the Windows system will temporarily save the file information we are about to delete under the "C:\Recycled" directory. Complete the file to be deleted in this directory through the "Empty Recycle Bin" button. The main idea of this procedure is based on the above knowledge. The details are as follows:

(1). First, determine whether the "recycled" directory below the C-packing directory is empty.

(2). If not empty, traverse all file information and delete the file.

   two. Programming and operating Environment

(1). Windows System 2000 Professional Edition

(2). Net FrameWork SDK Beta 2
three. Main steps and specific explanations for programming

(1). How to determine whether the "recycled" directory below the C-packing directory is empty.

To determine if there are any file information in the "C:\Recycled" directory, go through two steps, first to find the directory. Second, we can determine if this directory is empty. Following these two steps to do a specific introduction:

I&gt How to find the "C:\Recycled" directory in your program.

A namespace--system.io is encapsulated in the. Net FrameWork SDK. Many of the classes, objects, properties, and methods associated with file and directory processing are defined in this namespace. Visual C # is the flexibility to invoke these resources to implement specific operations on files and directories. This article mainly uses two classes in this namespace: The Directory class and the file class. The former is primarily used in programs to handle directory-related operations, such as determining directories and obtaining file information in a directory. The latter is primarily used in programs to handle file-related operations, such as deleting files. In a program using the GetFiles ("Directory Name") method in the Directory class, this method returns an enumerated type that consists of a set of strings. The GetFiles () method forms a string of each file name in the directory, which is then composed of these strings to form an enumeration type.

Ii> Determine if this directory is empty.

Here is a description of the foreach statement, which is used by Visual C # to collect enumerations. In this article, it is through him to enumerate all the deleted file information that exists in "C:\Recycled".
int c = 0; This variable is defined primarily to determine if there are files in the directory
foreach (String S1 in Directory.GetFiles ("c:\recycled"))//enum type that returns the file name string row
... {
++c;
}
if (C > 0)//To determine if there is a file if C > 0 The Recycle Bin has files, and vice versa
... {
.....
} (2). If the "C:\Recycled" directory is not empty, traverse all file information in the directory and delete the file.

In this paragraph, I want to introduce the use of the MessageBox class. People who have used VB and Delphi must know how easy it is to display a hint in such a programming language. In Visual C #, however, such operations are obviously relatively cumbersome, but they are somewhat more flexible. In Visual C #, a MessageBox class is encapsulated in the System.Windows.Forms namespace, which also defines some of the methods (show methods), objects, and properties associated with displaying the hint information. In this procedure there is a specific introduction to the MessageBox class.

To delete a file on a disk, you use the Delete () method of the file class in the namespace System.IO. The parameter followed by the Delete () method is the full path name of the disk on which the file resides. To get the full path name of the file, you also use the foreach statement, in which the foreach statement obtains the file's full path name from an enumeration type that contains all the file path names. The specific implementation statement is as follows:
if (C > 0)//Determine if file information exists in the "c:recycled" directory
... {
DialogResult r = MessageBox.Show ("Are you sure?) "," garbage disposal! " ,
Messageboxbuttons.yesno, messageboxicon.question);
The "OK" and "Cancel" two buttons are displayed, and the icon displays a question mark.
int ss= (int) R;
if (ss==6)//press OK button
... {foreach (string s in Directory.GetFiles ("c:\recycled"))
Put the full path name house s in
... {
File.delete (s); Delete this file
}
}
} three. Main steps and specific explanations for programming

(1). How to determine whether the "recycled" directory below the C-packing directory is empty.

To determine if there are any file information in the "C:\Recycled" directory, go through two steps, first to find the directory. Second, we can determine if this directory is empty. Following these two steps to do a specific introduction:

I&gt How to find the "C:\Recycled" directory in your program.

A namespace--system.io is encapsulated in the. Net FrameWork SDK. Many of the classes, objects, properties, and methods associated with file and directory processing are defined in this namespace. Visual C # is the flexibility to invoke these resources to implement specific operations on files and directories. This article mainly uses two classes in this namespace: The Directory class and the file class. The former is primarily used in programs to handle directory-related operations, such as determining directories and obtaining file information in a directory. The latter is primarily used in programs to handle file-related operations, such as deleting files. In a program using the GetFiles ("Directory Name") method in the Directory class, this method returns an enumerated type that consists of a set of strings. The GetFiles () method forms a string of each file name in the directory, which is then composed of these strings to form an enumeration type.

Ii> Determine if this directory is empty.

Here is a description of the foreach statement, which is used by Visual C # to collect enumerations. In this article, it is through him to enumerate all the deleted file information that exists in "C:\Recycled".
int c = 0; This variable is defined primarily to determine if there are files in the directory
foreach (String S1 in Directory.GetFiles ("c:\recycled"))//enum type that returns the file name string row
... {
++c;
}
if (C > 0)//To determine if there is a file if C > 0 The Recycle Bin has files, and vice versa
... {
.....
}2). If the "C:\Recycled" directory is not empty, traverse all file information in the directory and delete the file.

In this paragraph, I want to introduce the use of the MessageBox class. People who have used VB and Delphi must know how easy it is to display a hint in such a programming language. In Visual C #, however, such operations are obviously relatively cumbersome, but they are somewhat more flexible. In Visual C #, a MessageBox class is encapsulated in the System.Windows.Forms namespace, which also defines some of the methods (show methods), objects, and properties associated with displaying the hint information. In this procedure there is a specific introduction to the MessageBox class.

To delete a file on a disk, you use the Delete () method of the file class in the namespace System.IO. The parameter followed by the Delete () method is the full path name of the disk on which the file resides. To get the full path name of the file, you also use the foreach statement, in which the foreach statement obtains the file's full path name from an enumeration type that contains all the file path names. The specific implementation statement is as follows:
if (C > 0)//Determine if file information exists in the "c:recycled" directory
... {
DialogResult r = MessageBox.Show ("Are you sure?) "," garbage disposal! " ,
Messageboxbuttons.yesno, messageboxicon.question);
The "OK" and "Cancel" two buttons are displayed, and the icon displays a question mark.
int ss= (int) R;
if (ss==6)//press OK button
... {foreach (string s in Directory.GetFiles ("c:\recycled"))
Put the full path name house s in
... {
File.delete (s); Delete this file
}
}
} four. The source code of the program (Recycled.cs), the compiling method and the interface after the Operation

(1). The source code of the program: Recycled.cs:
Using System.IO;
Using System.Windows.Forms; Namespaces used in the import program
Class Recycle
... {
public static void Main ()
... {
int c = 0; This variable is defined primarily to determine if there are files in the directory
foreach (String S1 in Directory.GetFiles ("c:\recycled"))//enum type that returns the file name string row
... {
++c;
}
if (C > 0)//To determine if there is a file if C > 0 The Recycle Bin has files, and vice versa
... {
DialogResult r = MessageBox.Show ("Are you sure?) "," garbage disposal! " ,
Messageboxbuttons.yesno, messageboxicon.question);
The "OK" and "Cancel" two buttons are displayed, and the icon displays a question mark.
int ss= (int) R;
if (ss==6)//press OK button
... {foreach (string s in Directory.GetFiles ("c:\recycled"))
Put the full path name house s in
... {
File.delete (s); Delete this file
}
}
}
}
}

2). Compiling method:

After compiling from the following command line, a Windows execution file--recycled.exe is formed.

Csc/t:winexe/r:system.dll/r:system.windows.forms.dll Recycled.cs

Executing this file will have the following execution interface:

Figure 01: Program Execution Interface

Five. Summary :

This article is a software process to specify the power of Visual C # in the file processing, in fact, the above statement is only Visual C # in the file processing of a small application. Not only that, Visual C # also has powerful features in other areas. With this strong backing for the. Net FrameWork SDK, Visual C # makes it relatively easy to deal with difficult problems in previous programs, making it possible that the problems that were previously impossible to deal with, which is why Visual C # becomes the next-generation mainstream programming language.



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.