because the graduation project is based on U disk defense software, it involves some system application knowledge. This article is mainly about how to use C # code to empty the Recycle Bin resources. Mainly through the Shemptyrecyclebin function implementation. A. Shemptyrecyclebin function
the function of the Shemptyrecyclebin function is to empty the specified drive Recycle Bin. The function is as follows:
HRESULT Shemptyrecyclebin (
hwnd hwnd, //parent window handle
lpctstr Pszrootpath, ///The address of the Recycle Bin that will be emptied
DWORD dwflags //For emptying the Recycle Bin function parameters
);
The function is shell32.dll, where LPCTSTR pszrootpath points to the address of the Recycle Bin to be emptied, and if it is empty, clears the Recycle Bin on all drives. The dwflags parameter can have three different values, one or more combinations, Use to set whether to eject the System dialog box, whether to display the UI to delete the progress bar, and whether to play the sound when the deletion is complete.
const int sherb_noconfirmation = 0x000001; Do not show confirm Delete dialog box
const int sherb_noprogressui = 0x000002; Do not show the progress bar of the removal process
const int sherb_nosound = 0x000004; Do not play the sound when the deletion is complete
The shemptyrecyclebin for emptying files in the Recycle Bin is a kernel API method that requires a manual introduction of the class library of methods in C #. Also because the Windows API was invoked, So to add a reference to the System.Runtime.IneropServices namespace, the method's declaration syntax in C # is as follows:
DllImportAttribute ("Shell32.dll")] //DECLARE API function
private static extern int Shemptyrecyclebin (INTPTR handle, string root, int falgs);
two. Source Code
The program code is as follows:
using System using System.Collections.Generic; using System.ComponentModel; using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.Runtime.InteropServices; namespace Clearrecycle {public partial class Form1:form {const int sherb_noconfirmation = 0x000001; Do not show confirm Delete dialog box const int Sherb_noprogressui = 0x000002; Do not show the progress bar of the removal process const int sherb_nosound = 0x000004;
The sound public Form1 () {InitializeComponent () is not played when the deletion completes; [DllImportAttribute ("Shell32.dll")]//declaring API functions private static extern int Shemptyrecyclebin (INTP
TR handle, string root, int falgs); Click the "Empty Recycle Bin" button private void Button1_Click (object sender, EventArgs e) {Shemptyrecyclebin (this.
Handle, "", Sherb_noconfirmation + Sherb_noprogressui + sherb_nosound); }
}
}
The dwflags value in the code, "Sherb_noconfirmation + Sherb_noprogressui + sherb_nosound", means that the Confirm Delete dialog box is not displayed when the deletion occurs, and the progress bar for the deletion process is not displayed. No sound is played at the same time. If you do not specify a preset state, the System's dialog box and sound will pop up when you are 0. three. Operating Results
if Shemptyrecyclebin (this. Handle, "", 0) when prompted to confirm the Delete dialog box, if there are too many items, the deleted progress bar is displayed.
If the file is deleted, the sound is not displayed and can be modified by the Control Panel. The following illustration shows:
The article is mainly combined with their own graduation design part, while viewing the corresponding books and reference to the following bloggers to complete. is a basic application of the article, I hope you pay attention to the level of the article, his article is really useful, It seems to be a wise man in Taiwan. will also be the main object of my future study. Thank him very much for bringing such a useful series of articles. http://www.dotblogs.com.tw/larrynung/archive/2012/09/26/75065.aspx
Finally hope that the article for everyone to help, if there is insufficient or wrong place to forgive! As a result of the reference to several articles are reproduced, did not find the original author, but also thanks!
(By:eastmount 2014-1-17 9 o'clock in the evening http://blog.csdn.net/eastmount)