How can I tell that a directory is the recycle bin in C#? -


By looking at a folder, how can I tell if this is the recycle bin? I have not found anyone for C ++, but not for C #.

My first idea was to check for fileAttributes.System (which would be an acceptable estimate in my case), but in reality this flag recycle folder.

Crude solutions are using hardcoded folder names (we are all after 2009).

There is a small problem here The Windows Recycle Bin is a virtual folder and does not actually exist. The files that you see are not actually in that folder, they represent the existing files on the disk which have been replaced as a special name, which are removed from the "visible file system", but physical Not by appearance

You can "proof" for yourself by asking for the folder location using the Win32 API. It will return E_FAIL to the Recycle Bin, but not for other folders (can use all the content (see) and announcements required to run this code):

  Interputer Footer RecyclableBlink Path; // Try it to see it with KnownFolder.QuickLaunch: HRESULT hr = (HRESULT) SHGetKnownFolderPath (KnownFolder.RecycleBinFolder, 0, IntPtr.Zero, out ptrRecycleBinPath); If (hr == HRESULT.FIFEL) {Console.WriteLine ("A folder avaialable, virtual folder"); } And if (hr == HRESULT.S_OK) {string RecycleBinPath = Marshal.PtrToStringUni (ptrRecycleBinPath); Marshal.FreeCoTaskMem (ptrRecycleBinPath); Console.light line ("path:" + recyclable path); } // For convenience, if you paste follow-up announcements in your class, you can use the code directly above //: // Get a "known route" [DllImport ("shell32.dll" )] Steadily SHGetKnownFolderPath ([MarshalAs (UnmanagedType.LPSctrct)] Gid RFID, UIT Dvfags, Interactive HTTPS, Out Intercept pszPath); // Known Folder GUID Announcements Public Static Classes KnownFolder {// Many entries are present here, have been left for clarity, here to read public static Guid RecycleBinFolder = new Guid ("B7534046-3ECB-4C18-BE4E-64CD4CB7D6AC" ); Guid QuickLaunch = new Guid ("52a4f021-7b75-48a9- 9f6b-4b87a210bc8f") for public static reading; // ....} // COM invocations results: enum HRESULT: uint {S_FALSE = 0x0001, S_OK = 0x0000, E_FAIL = 0x80004005, E_INVALIDARG = 0x80070057, E_OUTOFMEMORY = 0x8007000E}  

For each drive the duplicate folder is repeated "$ Recycle.bin" . The hidden name is not stored in the registry and it can not be accessed by the API. Earlier it was reported that KnownFolderHelper will not retrieve this information (the same Lib is the designated method to get the Recycle Bin, also there is GetPath , it will be empty).

But not all this fake non-existent "filename" or "folder name" is a hidden file that looks like "S-1-5-21-2703390745 -3900912742-210389625-1000 " (Your will be different). It is one of the two "reliable" ways to know whether a certain filename is actually a virtual directory of the Recycle Bin (second way: delete a file, and check whether it is visible in your folder):

  string [] entries = directory. GetFileSystemEntries (@ "c: \ $ Recycle.bin", "? - - - - - - ?? *"); Note: I do not know that the hidden folders are on other Win32 versions, if you have a winner / no, no recycle bin  

L have to experiment a bit, they all have set systems and hidden flag and look like a disturbance GUID.

The API documentation is not very clear about it, but if you need confirmation, there is actually no way that can be recovered (it is very clear).

Update: , Alternate methods with ShellAPI.SHGetFolderLocation and all fail with it: either an empty result or error. I tested all the functions for the recycle bin and appadata (to make sure I use the correct parameters).

Only the documentation explicitly said, quote: "This function fails, except for UNC printer names, if the location specified by the Pidl parameter is not part of the file system." .


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -