none
C# 如何获取到回收站里面的文件信息 RRS feed

  • 问题

  • 在网上搜索了一大圈,没找到方法~
    1.Directory.GetFiles("回收站路径");有人使用"C:\\RECYCLER",这个毫无疑问错误的。
    又去搜索获取回收站路径,有个API:SHGetSpecialFolderPath 但是获取回收站路径为空,不知道为什么,有人可以解释下当然最好
    2.SHQueryRecycleBin和SHEmptyRecycleBin API ,但是只能实现获取回收站中占用空间大小和文件个数,以及清空回收站的功能,不符合要求。
    先在此谢谢了…
    2013年8月16日 3:24

答案

  • 您好,

    請參考,

    建立console專案 ConsoleApplication1,

    1.專案中加入 C:\windows\system32\shell32.dll

    2.建立RecycleBin class,code如下(copy from Working With The Windows Recycle Bin With C#)

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Shell32;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication1
    {
        public class RecycleBinItem
        {
            public string FileName { get; set; }
            public string FileType { get; set; }
            public string FileSize { get; set; }
        }
    
        public class RecycleBin
        {
             
            public List<RecycleBinItem> GetRecycleBinItems()
            {
                try
                {
                    //create a new isntance of the Shell32 interface
                    Shell shell = new Shell();
                    List<RecycleBinItem> list = new List<RecycleBinItem>();
    
                    //create a folder item to our Recycle Bin
                    Folder recycleBin = shell.NameSpace(10);
    
                    //now let's loop through all the Items in our Folder object
                    //and add them to a generic list
                    foreach (FolderItem2 f in recycleBin.Items())
                        list.Add(
                                new RecycleBinItem
                                {
                                    FileName = f.Name,
                                    FileType = f.Type,
                                    FileSize = GetSize(f).ToString()
                                });
                    //return the list
                    return list;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("Error accessing the Recycle Bin: {0}", ex.Message));
                    return null;
                }
            }
    
            public double GetSize(FolderItem folderItem)
            {
                //check if it's a folder, if it's not then return it's size
                if (!folderItem.IsFolder)
                    return (double)folderItem.Size;
    
                //create a new Shell32.Folder item
                Folder folder = (Folder)folderItem.GetFolder;
    
                double size = 0;
    
                //since we're here we're dealing with a folder, so now we will loop
                //through everything in it and get it's size, thus calculating the
                //overall size of the folder
                foreach (FolderItem2 f in folder.Items())
                    size += GetSize(f);
    
                //return the size
                return size;
    
            }
    
    
        }
    }
    
    
    


    3.main程式如下,

    using System;
     
    //C# 如何获取到回收站里面的文件信息
    //http://social.msdn.microsoft.com/Forums/zh-TW/cf19df07-14c3-4411-b8bd-8d5b18e01b3b/c-
    namespace ConsoleApplication1
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                RecycleBin bin = new RecycleBin();
                var items = bin.GetRecycleBinItems();
                foreach (RecycleBinItem item in items)
                    Console.WriteLine(string.Format("File: {0} => Size: {1} => Type: {2}",
                        item.FileName, item.FileSize, item.FileType));
    
    
                Console.ReadLine();
            }
        }
    }
    


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/

    2013年8月16日 4:16

全部回复

  • 利用Windows Shell 编程接口来获取回收站中的文件和在删除前的原路径
    1、IShellFolder接口
      IShellFolder是Windows Shell 程序对目标进行管理的一个重要接口。每一个目录对于一个
      实例化的IShellFolder接口,包括EnumObject、GetAttributesOf、GetDisplayNameOf等
    2、IEnumIDList 接口
         这个借口提供了一组标准的方法,用于遍历PIDL,包括Clone、Next、Reset、Skip等
    DWORD ListFileInRecycleBin()
    {
    TCHAR pszPath[MAX_PATH];  // 保存路径
    // IShellFolder接口
    IShellFolder *pisf = NULL;
    IShellFolder *pisfRecBin = NULL;
    // 获取“根”目录,桌面
    SHGetDesktopFolder(&pisfRecBin);

    IEnumIDList *peidl = NULL; // 对象遍历接口
    LPITEMIDLIST pidlBin = NULL;
    LPITEMIDLIST idlCurrent = NULL;

    LPMALLOC pMalloc = NULL;
    // 分配
    SHGetMalloc(&pMalloc);
    // 回收站位置
    SHGetFolderLocation(NULL, CSIDL_BITBUCKET, NULL, 0, &pidlBin);
    // 绑定回收站对象
    pisfRecBin->BindToObject(pidlBin,NULL,IID_IShellFolder,(void **) &pisf);
    // 列举回收站中的对象,得到IEnumIDList接口,包括SHCONTF_FOLDERS、
    // SHCONTF_NONFOLDERS、SHCONTF_INCLUDEHIDDEN类型的对象
    pisf->EnumObjects(NULL,
      SHCONTF_FOLDERS | SHCONTF_NONFOLDERS |SHCONTF_INCLUDEHIDDEN,
      &peidl);

    STRRET strret;
    ULONG uFetched;

    HANDLE hOutPut = GetStdHandle(STD_OUTPUT_HANDLE);
    printf("\nFiles In Recycle Bin:\n");

    while(1)
    {
      // 遍历IEnumIDList对象,idlCurrent为当前对象
      if(peidl->Next(1,&idlCurrent,&uFetched) == S_FALSE)
       break;
      // 获取回收站当前对象当前的路径,这里没有输出结果,读者可自行修改
      SHGetPathFromIDList(idlCurrent,  pszPath);
      // DisplayName,删除前的路径
      pisf->GetDisplayNameOf(idlCurrent,SHGDN_NORMAL,&strret);
      // 显示,printf可能会造成字符编码不正确。
      WriteConsoleW(hOutPut,L"\t",1,NULL,NULL);
      WriteConsoleW(hOutPut,strret.pOleStr,lstrlenW(strret.pOleStr),NULL,NULL);
      WriteConsoleW(hOutPut,L"\n",1,NULL,NULL);
    }
    // 释放资源
    pMalloc->Free(pidlBin);
    pMalloc->Free(strret.pOleStr);
    pMalloc->Release();
    peidl->Release();
    pisf->Release();
    return 0;
    }

    代码来源于《精通Windows API - 函数 接口 编程实例》一书
    2013年8月16日 3:31
  • 利用Windows Shell 编程接口来获取回收站中的文件和在删除前的原路径
    1、IShellFolder接口
      IShellFolder是Windows Shell 程序对目标进行管理的一个重要接口。每一个目录对于一个
      实例化的IShellFolder接口,包括EnumObject、GetAttributesOf、GetDisplayNameOf等
    2、IEnumIDList 接口
         这个借口提供了一组标准的方法,用于遍历PIDL,包括Clone、Next、Reset、Skip等
    DWORD ListFileInRecycleBin()
    {
    TCHAR pszPath[MAX_PATH];  // 保存路径
    // IShellFolder接口
    IShellFolder *pisf = NULL;
    IShellFolder *pisfRecBin = NULL;
    // 获取“根”目录,桌面
    SHGetDesktopFolder(&pisfRecBin);

    IEnumIDList *peidl = NULL; // 对象遍历接口
    LPITEMIDLIST pidlBin = NULL;
    LPITEMIDLIST idlCurrent = NULL;

    LPMALLOC pMalloc = NULL;
    // 分配
    SHGetMalloc(&pMalloc);
    // 回收站位置
    SHGetFolderLocation(NULL, CSIDL_BITBUCKET, NULL, 0, &pidlBin);
    // 绑定回收站对象
    pisfRecBin->BindToObject(pidlBin,NULL,IID_IShellFolder,(void **) &pisf);
    // 列举回收站中的对象,得到IEnumIDList接口,包括SHCONTF_FOLDERS、
    // SHCONTF_NONFOLDERS、SHCONTF_INCLUDEHIDDEN类型的对象
    pisf->EnumObjects(NULL,
      SHCONTF_FOLDERS | SHCONTF_NONFOLDERS |SHCONTF_INCLUDEHIDDEN,
      &peidl);

    STRRET strret;
    ULONG uFetched;

    HANDLE hOutPut = GetStdHandle(STD_OUTPUT_HANDLE);
    printf("\nFiles In Recycle Bin:\n");

    while(1)
    {
      // 遍历IEnumIDList对象,idlCurrent为当前对象
      if(peidl->Next(1,&idlCurrent,&uFetched) == S_FALSE)
       break;
      // 获取回收站当前对象当前的路径,这里没有输出结果,读者可自行修改
      SHGetPathFromIDList(idlCurrent,  pszPath);
      // DisplayName,删除前的路径
      pisf->GetDisplayNameOf(idlCurrent,SHGDN_NORMAL,&strret);
      // 显示,printf可能会造成字符编码不正确。
      WriteConsoleW(hOutPut,L"\t",1,NULL,NULL);
      WriteConsoleW(hOutPut,strret.pOleStr,lstrlenW(strret.pOleStr),NULL,NULL);
      WriteConsoleW(hOutPut,L"\n",1,NULL,NULL);
    }
    // 释放资源
    pMalloc->Free(pidlBin);
    pMalloc->Free(strret.pOleStr);
    pMalloc->Release();
    peidl->Release();
    pisf->Release();
    return 0;
    }

    代码来源于《精通Windows API - 函数 接口 编程实例》一书
    不好意思,这个看不懂,能不能用C#转一下,谢谢…
    2013年8月16日 3:34
  • 您好,

    請參考一下,

    Working With The Windows Recycle Bin With C#

    [STAThread]
    static void Main(string[] args)
    {
    	RecycleBin bin = new RecycleBin();
    	var items = bin.GetRecycleBinItems();
    	foreach (RecycleBinItem item in items)
    		Console.WriteLine(string.Format("File: {0} => Size: {1} => Type: {2}",
    			item.FileName, item.FileSize, item.FileType));
    
    
    	Console.ReadLine();
    }

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/b25e2b8f-141a-4a1c-a73c-1cb92f953b2b/instantiate-shell32shell-object-in-windows-8


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/

    2013年8月16日 3:47
  • 您好,

    請參考一下,

    Working With The Windows Recycle Bin With C#

    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/

    不好意思,上面那个连接打不开…
    2013年8月16日 4:01
  • 您好,

    請參考,

    建立console專案 ConsoleApplication1,

    1.專案中加入 C:\windows\system32\shell32.dll

    2.建立RecycleBin class,code如下(copy from Working With The Windows Recycle Bin With C#)

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Shell32;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication1
    {
        public class RecycleBinItem
        {
            public string FileName { get; set; }
            public string FileType { get; set; }
            public string FileSize { get; set; }
        }
    
        public class RecycleBin
        {
             
            public List<RecycleBinItem> GetRecycleBinItems()
            {
                try
                {
                    //create a new isntance of the Shell32 interface
                    Shell shell = new Shell();
                    List<RecycleBinItem> list = new List<RecycleBinItem>();
    
                    //create a folder item to our Recycle Bin
                    Folder recycleBin = shell.NameSpace(10);
    
                    //now let's loop through all the Items in our Folder object
                    //and add them to a generic list
                    foreach (FolderItem2 f in recycleBin.Items())
                        list.Add(
                                new RecycleBinItem
                                {
                                    FileName = f.Name,
                                    FileType = f.Type,
                                    FileSize = GetSize(f).ToString()
                                });
                    //return the list
                    return list;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("Error accessing the Recycle Bin: {0}", ex.Message));
                    return null;
                }
            }
    
            public double GetSize(FolderItem folderItem)
            {
                //check if it's a folder, if it's not then return it's size
                if (!folderItem.IsFolder)
                    return (double)folderItem.Size;
    
                //create a new Shell32.Folder item
                Folder folder = (Folder)folderItem.GetFolder;
    
                double size = 0;
    
                //since we're here we're dealing with a folder, so now we will loop
                //through everything in it and get it's size, thus calculating the
                //overall size of the folder
                foreach (FolderItem2 f in folder.Items())
                    size += GetSize(f);
    
                //return the size
                return size;
    
            }
    
    
        }
    }
    
    
    


    3.main程式如下,

    using System;
     
    //C# 如何获取到回收站里面的文件信息
    //http://social.msdn.microsoft.com/Forums/zh-TW/cf19df07-14c3-4411-b8bd-8d5b18e01b3b/c-
    namespace ConsoleApplication1
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                RecycleBin bin = new RecycleBin();
                var items = bin.GetRecycleBinItems();
                foreach (RecycleBinItem item in items)
                    Console.WriteLine(string.Format("File: {0} => Size: {1} => Type: {2}",
                        item.FileName, item.FileSize, item.FileType));
    
    
                Console.ReadLine();
            }
        }
    }
    


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/

    2013年8月16日 4:16
  • 您好,

    請參考,

    建立console專案 ConsoleApplication1,

    1.專案中加入 C:\windows\system32\shell32.dll

    2.建立RecycleBin class,code如下(copy from Working With The Windows Recycle Bin With C#)


    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/

    非常感谢,可以得到…
    2013年8月16日 5:09