C# 如何从回收站还原文件
可能重复:C# 如何从回收站还原文件,c#,winapi,file,restore,recycle-bin,C#,Winapi,File,Restore,Recycle Bin,可能重复: 有人知道如何使用C#with Windows API从回收站还原文件?这可以帮助您 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.InteropServi
有人知道如何使用C#with Windows API从回收站还原文件?这可以帮助您
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;
namespace RecyclerCS
{
public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
}
private Shell Shl;
private const long ssfBITBUCKET = 10;
private const int recycleNAME = 0;
private const int recyclePATH = 1;
private void button1_Click(object sender, System.EventArgs e) {
string S = "This is text in the file to be restored from the Recycle Bin.";
string FileName = "C:\\Temp\\Text.txt";
File.WriteAllText(FileName, S);
Delete(FileName);
MessageBox.Show(FileName + " has been moved to the Recycle Bin.");
if (Restore(FileName))
MessageBox.Show(FileName + " has been restored");
else
MessageBox.Show("Error");
Marshal.FinalReleaseComObject(Shl);
}
private void Delete(string Item) {
FileSystem.DeleteFile(Item, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
//Gives the most control of dialogs.
}
private bool Restore(string Item) {
Shl = new Shell();
Folder Recycler = Shl.NameSpace(10);
for (int i = 0; i < Recycler.Items().Count; i++) {
FolderItem FI = Recycler.Items().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 (Item == Path.Combine(FilePath, FileName)) {
DoVerb(FI, "ESTORE");
return true;
}
}
return false;
}
private 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;
}
}
}
使用系统;
使用系统集合;
使用System.Windows.Forms;
使用System.IO;
使用Shell32//参考COM选项卡上的Microsoft Shell控件和自动化。
使用System.Runtime.InteropServices;
使用Microsoft.VisualBasic.FileIO;
命名空间回收器
{
公共部分类Form1:Form
{
公共表格1(){
初始化组件();
}
私人壳牌Shl;
私有const long ssfBITBUCKET=10;
私有常量int recycleNAME=0;
私有const int recyclePATH=1;
私有无效按钮1\u单击(对象发送者,System.EventArgs e){
string S=“这是要从回收站还原的文件中的文本。”;
字符串FileName=“C:\\Temp\\Text.txt”;
writealText(文件名,S);
删除(文件名);
Show(FileName+“已移动到回收站。”);
如果(还原(文件名))
MessageBox.Show(文件名+“已还原”);
其他的
MessageBox.Show(“错误”);
最终选举对象(Shl)元帅;
}
私有无效删除(字符串项){
DeleteFile(Item,UIOption.OnlyErrorDialogs,RecycleOption.SendToRecycleBin);
//提供对对话框的最大控制。
}
私有布尔恢复(字符串项){
Shl=新外壳();
文件夹回收器=Shl.NameSpace(10);
对于(int i=0;i
在这里看一下这个项目:
壳牌公司,第2部分:
@YetAnotherUser:天哪,我没看到。。。宽恕。不管怎样,托马斯的回答是正确的和不同的。我想我必须检查一下。。。谢谢。@Thomas:神圣的天堂,在你给我的链接中,是解决方案,请原谅。@Krähne:我已经更新了我的答案,检查一下。@Thomas:完美现在可以了。谢谢!“ESTORE”不适用于非英语窗口。