再次引用shell32,C# Visual Studio
嗯。好吧,重新访问 PInvoke 后,我确信我不太明白:-/ (只是问 这个问题)
让我说明一下我需要处理的代码。当我使用“添加引用 --> COM --> Microsoft Shell 控制和自动化”时它可以工作...但遗憾的是它在我的项目中放置了一个引用,如下所示:“C:\Users\Tim\Documents\ Visual Studio 2008\Projects\Wing\FileWing\obj\Debug\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;
}
}
Hmmm. Okay after revisiting PInvoke, I'm sure that I don't quite get it :-/ (just asked this question)
Let me illustrate the code I need to handle. It works when I use "Add Reference --> COM --> Microsoft Shell Controls and Automatation" ... but sadly it places a reference in my project that looks like this: "C:\Users\Tim\Documents\Visual Studio 2008\Projects\Wing\FileWing\obj\Debug\Interop.Shell32.dll"
I'm digging though the recycling bin and seek for a item that I want to recover. Is there any way NOT fighting through the PInvoke to get this done? Or to get a reference to the system32/shell32.dll that lets me use this code at runtime?
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在您正在混合 2 个不同的概念:PInvoke 和 COM Interop。
PInvoke 允许您从托管代码中访问本机 C 函数。它的工作原理是在托管代码中定义本机方法的编组兼容签名,并使用
DllImport
属性对其进行标记。它需要但不能拥有对本机 DLL 的元数据引用。使用 Win32 DLL 的正常加载规则在运行时发现 DLL。COM Interop 允许您从托管代码访问 COM 兼容对象。这是通过获取 COM 接口的编组兼容托管定义,然后通过多种方式之一获取对该对象的引用来完成的。获取托管定义通常是通过添加对 COM 组件的 PIA(主互操作程序集)的元数据引用来完成的。在 C# 4.0 之前,无需进行大量工作就无法删除此引用,并且必须与您的应用程序一起部署。
在此特定示例中,您使用的是 COM 互操作而不是 PInvoke。
Right now you're mixing 2 different concepts: PInvoke and COM Interop.
PInvoke allows you to access native C functions from within managed code. It works by defining a marshal compatible signature of the native method in managed code and marking it with the
DllImport
attribute. It requires, and cannot have, a metadata reference to the native DLL. The DLL is discovered at runtime using normal loading rules for a Win32 DLL.COM Interop allows you to access COM compatible objects from managed code. This is done by getting a marshal compatible managed definition of the COM interface and then gaining a referece to the object in one of several ways. Getting the managed definition is often accomplished by means of adding a metadata reference to the PIA (primary interop assembly) for the COM component. Until C# 4.0, this reference cannot be removed, without a lot of work, and must be deployed with your application.
In this particular example you are using COM interop and not PInvoke.