嗯。好了重新审视的PInvoke后,我敢肯定,我不太明白: - /(刚才问的这个问题

让我说明我需要处理的代码。它的工作原理,当我使用“添加引用 - > COM - >微软壳牌控制和Automatation” ......但遗憾的是它的地方在我的项目的参考,看起来像这样:“C:\用户\添\文档\ Visual Studio中2008 \项目\翼\ FileWing \ OBJ \调试\ Interop.Shell32.dll“

我挖尽管回收站,并寻求,我想恢复项目。有没有办法通过的PInvoke不战来完成这件事?或获得到system32参考/ shell32.dll中,让我用这个代码在运行时?

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;
    }
}
有帮助吗?

解决方案

现在你混合2个不同的概念:PInvoke的和COM互操作。

的PInvoke允许从托管代码内访问本地C函数。它通过定义托管代码的本地方法的元帅兼容的签名,并与DllImport属性标记它。它要求,而且不能有,元数据参考本地DLL。该DLL使用正常的加载规则为一个Win32 DLL在运行时发现的。

COM互操作允许用户从托管代码访问COM兼容的对象。这是通过获取COM接口的兼容元帅管理的定义,然后获得一个referece对象中的几种方式之一来完成。获取管理定义常常通过添加COM组件元数据参照PIA(主互组件)来完成。直到C#4.0,此参考不能被删除,没有大量的工作,而且必须与应用程序部署。

在此特定示例中使用的是COM互操作,而不是PInvoke的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top