将文件移动到回收站和清空回收站都有很好的文档记录,但是如何通过编程从回收站恢复文件?
发布于 2009-05-26 16:01:17
在纯C#中似乎没有解决方案。您很可能不得不求助于P/Invoke。This article提供了一个使用SHFileOperation应用编程接口的C++解决方案。
发布于 2009-05-26 16:10:38
除了前面提到的codeproject链接之外,我能看到的唯一其他引用提到了这一点:
调用传递CSIDL_BITBUCKET的SHGetFolderLocation。然后,您可以像往常一样操作该文件夹。您必须为SHGetFolderLocation函数创建一个互操作。
"CSIDL_BUCKET“是虚拟RecycleBin文件夹的常量。本文引用自here,并将涉及到与Windows shell的互操作。MSDN还提到,为了支持Vista中的另一个函数,该函数已被弃用。
发布于 2020-06-10 17:08:23
希望下面的代码将恢复文件。请确保,STA调用仅支持shell调用
using System;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using Shell32; //Reference Microsoft Shell Controls And Automation on the COM tab.
using System.Runtime.InteropServices;
using Microsoft.VisualBasic.FileIO;
using System.Threading;
private static void Restore(object param)
{
object[] args = (object[])param;
string filename = (string)args[0];
string filepath = (string)args[1];
Shl = new Shell();
Folder Recycler = Shl.NameSpace(10);
var c = Recycler.Items().Count;
var _recycler = Recycler.Items();
for (int i = 0; i < _recycler.Count; i++)
{
FolderItem FI = _recycler.Item(i);
string FileName = Recycler.GetDetailsOf(FI, 0);
if (Path.GetExtension(FileName) == "") FileName += Path.GetExtension(FI.Path);
//Necessary for systems with hidden file extensions.
string FilePath = Recycler.GetDetailsOf(FI, 1);
if (filepath == Path.Combine(FilePath, FileName))
{
DoVerb(FI, "ESTORE");
break;
}
}
}
private static bool DoVerb(FolderItem Item, string Verb)
{
foreach (FolderItemVerb FIVerb in Item.Verbs())
{
if (FIVerb.Name.ToUpper().Contains(Verb.ToUpper()))
{
FIVerb.DoIt();
return true;
}
}
return false;
}https://stackoverflow.com/questions/911391
复制相似问题