How to C # programmatically or command line open explorer.exe in the "Trash",

I want a WPF button that will open explorer.exe on Windows 7 | 8 straight to the "basket". This is because my application erases many files, and I want to provide the user with a quick way to recover files. Command line arguments do not work, possibly because the Recycle Bin is a virtual directory. I tried using the "$ Recycle Bin". Explorer.exe / root, where a is a virtual file. Trying to protect space in Recycle \ Bin does not work either.

Here is the working code of Scott Powell that I tested and use. Thanks Scott @

private void ExploreTrashBin ( ) { String str_RecycleBinDir = String.Format(@"C:\$Recycle.Bin\{0}", UserPrincipal.Current.Sid); Process . Start ( "explorer.exe" , str_RecycleBinDir ); } private void TrashBin_Button_Click ( object sender , RoutedEventArgs e ) { ExploreTrashBin ( ); } 
+6
source share
3 answers

You can run the following command to achieve this,

 start shell:RecycleBinFolder 

From your C # code that you can use,

 System.Diagnostics.Process.Start("explorer.exe", "shell:RecycleBinFolder"); 
+5
source

It is already implemented in the Microsoft.VisualBasic.FileIO.FileSystem class in .Net (so C # natively supports using this ).

This way you do not need to run the command shell : just delete files / folders programmatically , as if done interactively using Windows Explorer!

 using Microsoft.VisualBasic.FileIO; FileSystem.DeleteFile(...) FileSystem.DeleteDirectory(...) 

enter image description here

+1
source

The Recycle Bin is located in a hidden directory named \ $ Recycle.Bin \% SID%, where% SID% is the SID of the user who performed the deletion.

So, based on this, we can: Add a .NET link to System.DirectoryServices.AccountManagement

 string str_RecycleBinDir = UserPrincipal.Current.Sid; Process.Start("explorer.exe","C:\$Recycle.Bin\" + str_RecycleBinDir); 

It should be able to now access the appropriate basket directory based on the running user account. Work in Windows 7 (verified).

0
source

Source: https://habr.com/ru/post/981946/


All Articles