再次引用shell32,C#Visual Studio

Akk*_*kku 6 c# shell32 reference visual-studio-2008

嗯.好的,重新访问PInvoke后,我确信我不太明白: - /(刚问过这个问题)

让我举例说明我需要处理的代码.当我使用"添加引用 - > COM - > Microsoft Shell控件和自动化"时它可以工作......但遗憾的是它在我的项目中放置了一个如下所示的引用:"C:\ Users\Tim\Documents\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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Jar*_*Par 7

现在你混合了两个不同的概念:PInvoke和COM Interop.

PInvoke允许您从托管代码中访问本机C函数.它的工作原理是在托管代码中定义本机方法的编组兼容签名,并使用该DllImport属性对其进行标记.它需要,也不能具有对本机DLL的元数据引用.在运行时使用Win32 DLL的常规加载规则发现DLL.

COM Interop允许您从托管代码访问COM兼容对象.这是通过获取COM接口的marshal兼容托管定义,然后以多种方式之一获取对象的引用来完成的.获取托管定义通常是通过向COM组件的PIA(主互操作程序集)添加元数据引用来实现的.在C#4.0之前,如果没有大量工作,则无法删除此引用,并且必须随应用程序一起部署.

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