如何使用 C# 从回收站恢复文件?

发布于 2024-07-21 07:22:11 字数 48 浏览 10 评论 0原文

将文件移动到回收站和清空回收站都有详细记录,但是如何以编程方式从回收站恢复文件呢?

Moving files to the recycle bin and emptying the recycle bin are well documented, but how can a file be programmatically restored from the recycle bin?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执着的年纪 2024-07-28 07:22:11

纯C#似乎没有解决方案。 您很可能必须求助于 P/Invoke。
本文使用 SHFileOperation API。

There seems not to be a solution in pure C#. You most likely have to resort to P/Invoke.
This article presents a solution in C++ using the SHFileOperation API.

来世叙缘 2024-07-28 07:22:11

除了前面提到的 codeproject 链接之外,我可以找到的唯一其他参考看到提到这一点:

调用 SHGetFolderLocation 传递 CSIDL_BITBUCKET
然后您可以像往常一样操作该文件夹。
您必须为 SHGetFolderLocation 函数创建互操作。

CSIDL_BITBUCKET 是 CSIDL(“常量特殊项”) ID 列表”)虚拟回收站文件夹的值。 引用摘自此处,并且将涉及与 Windows shell 的互操作。 MSDN 还提到此功能已被弃用支持 Vista 中的另一个。

The only other reference to this beyond the previously mentioned link to codeproject that I can see mentions this:

Call SHGetFolderLocation passing CSIDL_BITBUCKET.
Then you can manipulate that folder as usual.
You'll have to create an interop for the SHGetFolderLocation function.

CSIDL_BITBUCKET being the CSIDL ("constant special item ID list") value for the virtual Recycle Bin folder. The quote is taken from here, and will involve interop with the Windows shell. MSDN also mentions that this function has been deprecated in favour of another in Vista.

葬シ愛 2024-07-28 07:22:11

希望下面的代码能够恢复文件。 请确保 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;
    }

Hope below code will work to restore the files. Please make sure, STA Calls only supported for shell calls

     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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文