How to close the Recycle Bin using VB.Net or C#.Net
i would like to call a function that Closes the recycle bin window. This is the code that opens the recycle bin, however i can't find the code that closes it :
Process.Start("explorer.exe", "/n, ::{645FF040开发者_如何学运维-5081-101B-9F08-00AA002F954E}")
Well as Adam has specified you need to use findwindow and sendmessage API to find and then close the window.
Here's a sample code
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
Public Const WM_SYSCOMMAND As Integer = &H112
Public Const SC_CLOSE As Integer = &HF060
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim hwnd As IntPtr
hwnd = FindWindow("CabinetWClass", "Recycle Bin")
if hwnd = Nothing then
MessageBox.Show("Recycle Bin not found.")
else
SendMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0)
end if
End Sub
End Class
Converting the code to c# is pretty easy, still let me know if you face any problem while converting the code.
Source : Code Project
Edit 1 : You can use Spy++ to find the classname and windowname provided with .net tools.
If it has been opened by someone else you might need to use the win32 api - eg. findwindow then sendmessage to send the close message.
Process.Start
returns you the Process
started. Try this:
Process p=Process.Start("explorer.exe",
"/n, ::{645FF040-5081-101B-9F08-00AA002F954E}");
p.CloseMainWindow();
//OR
p.Close();
精彩评论