3

我需要参考 system32/shell32.dll,因为我使用一些 shell 函数来读取回收站。我尝试了“添加引用--> COM--> Microsoft Shell 控件和自动化”和“添加引用--> 浏览--> [直接转到 system32/shell32.dll]。两者都将 shell32 引用添加到我的引用中. 但是当我查看属性时,我看到引用的路径如下所示:“C:\Users\Tim\Documents\Visual Studio 2008\Projects\Wing\FileWing\obj\Debug\Interop.Shell32.dll” ...

我不会将此 \obj\Debug\ 路径部署到我的安装程序。那么如何直接引用最终用户的 shell32.dll 呢?有办法吗?为什么VS2008会创建这个奇怪的路径?我可以更改此路径,使其不位于这个奇怪的子文件夹中吗?


嗯。好的,在重新访问 PInvoke 之后,我确定我不太明白:-/

让我说明我需要处理的代码。我正在翻找回收箱,寻找我想要回收的物品。有没有办法不通过 PInvoke 来完成这项工作?

    private void recoverRecyclerBinEntry(string fileName, int size)
    {
        try
        {
            Shell Shl = new Shell();
            Folder Recycler = Shl.NameSpace(10);

            // scans through all the recyclers entries till the one to recover has been found
            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);
                string combinedPath = Path.Combine(FilePath, FileName);

                if (size == FI.Size && fileName == combinedPath)
                {
                    Debug.Write("Match found. Restoring " + combinedPath + "...");
                    Undelete(FI);
                    Debug.WriteLine("done.");
                }
                else
                {
                    Debug.WriteLine("No match");
                }
            }
        } 
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            Debug.WriteLine(ex.StackTrace);
        }
    }

    private bool Undelete(FolderItem Item)
    {
        try
        {
            foreach (FolderItemVerb FIVerb in Item.Verbs())
            {
                if (
                    (FIVerb.Name.ToUpper().Contains("WIEDERHERSTELLEN")) ||
                    (FIVerb.Name.ToUpper().Contains("ESTORE")) ||
                    (FIVerb.Name.ToUpper().Contains("NDELETE"))
                    )
                {
                    FIVerb.DoIt();
                    return true;
                }
            }
            //execute the first one:
            Item.Verbs().Item(0).DoIt();
            return true;
        }
        catch (Exception)
        {
            Debug.WriteLine("ERROR undeleting");
            return false;
        }
    }
4

3 回答 3

5

我相信您正在寻找P/Invoke (Platform Invoke)

一旦您了解了包含和使用 DLL 的方法,您可以访问pinvoke.net以获取使用某些方法的特定代码片段。

于 2010-01-25T15:40:30.003 回答
2

你只是DllImport用来访问 shell32/kernel32 中的功能吗?如果是这样,您不需要添加参考。

例如:

[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW",  SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool MoveFile(String src, String dst);

这是有关使用平台调用的教程,这是 MSDN文章

于 2010-01-25T15:41:28.520 回答
0

使用 VS 2008 添加 dll 引用后,您可以打开 .dll 的属性。

确保将本地复制设置为真。

如果这不起作用,另一种解决方案是将 .dll 作为项目添加到您的项目中,并将 make 作为内容,并告诉它复制到输出目录。

于 2010-01-25T15:41:14.230 回答