【问题标题】:How to restore files from recycle bin [duplicate]如何从回收站恢复文件[重复]
【发布时间】:2011-05-17 01:28:46
【问题描述】:

可能重复:
How do I restore a file from the recycle bin using C#?

有人知道如何使用 C# 和 Windows API 从回收站恢复文件吗?

【问题讨论】:

  • @YetAnotherUser:天哪,我没有看到……宽恕。无论如何,托马斯的反应是正确的和不同的。

标签: c# winapi file restore recycle-bin


【解决方案1】:

这个link可以帮助你

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;
    }
  }
}

【讨论】:

  • @Thomas:天哪,在你给我的链接中,是解决方案,请见谅。
  • @Krähne:我已经更新了我的答案,检查一下。
  • @Thomas:完美现在有效。非常感谢!
  • "ESTORE" 不适用于非英语 Windows。
【解决方案2】:

在这里看看这个项目:Codeproject

C# 做 Shell,第 2 部分:

【讨论】:

  • 我想我必须检查一下...谢谢。
猜你喜欢
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多