Как да възстановите файлове от кошчето

Възможен дубликат:
Как да възстановя файл от кошчето с помощта на C#?

Някой знае как да възстанови файлове от кошчето с помощта на C# с Windows API?.


person Krähne    schedule 17.05.2011    source източник
comment
@YetAnotherUser: Господи, не видях ... прошка. Както и да е, отговорът на Томас е правилен и различен.   -  person Krähne    schedule 17.05.2011


Отговори (2)


Тази връзка може да ви помогне

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;
    }
  }
}
person jams    schedule 17.05.2011
comment
@Thomas: За бога, във връзката, която ми даде, е решението, моля, извинете ме. - person Krähne; 17.05.2011
comment
@Krähne: Актуализирах отговора си, проверете го. - person jams; 17.05.2011
comment
@Thomas: Идеално сега работи. Благодаря много! - person Krähne; 17.05.2011
comment
ESTORE не е приложим за неанглийски Windows. - person YFdyh000; 01.04.2020

Разгледайте този проект тук: Codeproject

C# прави Shell, част 2:

въведете описание на изображението тук

person Tristan Dubé    schedule 17.05.2011
comment
Предполагам, че трябва да го проверя... благодаря. - person Krähne; 17.05.2011