Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to c# programmatically or command line open explorer.exe to the "Recycle Bin"

Tags:

c#

windows

wpf

I want a WPF button that will open explorer.exe in Windows 7|8 directly into the "Recycle Bin". This is because my app erases a lot of files, and I want to provide the user with a quick way to restore files. The command line arguments don't work, possibly because the "Recycle Bin" is a virtual directory. I have tried using "$Recycle Bin". Explorer.exe /root, where a is a virtual file fails. Trying to protect the space in Recycle\ Bin does not seem to work as well.

Here is working code from Scott Powell that I tested and am using. Thank you 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 ( );
        }
like image 775
dr d b karron Avatar asked Feb 04 '15 01:02

dr d b karron


People also ask

Can I learn C as a beginner?

Indeed, it is strongly recommended to start your programming journey with C language as it helps to understand a lot of underlying processes on the ground level, which enhances your fundamental knowledge & boosts your confidence, which further makes it easier for you to learn other high-level programming languages as ...

Is C difficult to learn?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

You could execute following command in order to achieve this,

start shell:RecycleBinFolder

From your C# code you could use,

System.Diagnostics.Process.Start("explorer.exe", "shell:RecycleBinFolder");
like image 53
Low Flying Pelican Avatar answered Oct 17 '22 21:10

Low Flying Pelican


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

This way, you don't need run shell command : just delete files/folders programmatically as if done interactively with Windows Explorer!

using Microsoft.VisualBasic.FileIO;

FileSystem.DeleteFile(...)
FileSystem.DeleteDirectory(...)

enter image description here

like image 39
David Avatar answered Oct 17 '22 22:10

David