我有这段代码运行良好,并为我提供了用户开始菜单的路径:
Dim oShell As Object = CreateObject("Shell.Application")
MsgBox(oShell.NameSpace(11).Self.Path)
这显然使用了后期绑定(bind)。现在说我想在 C# 或 VB.NET 严格模式下执行此操作,它们都不支持这种带有后期绑定(bind)的语法。
这可能吗?如何?
谢谢你的帮助!
最佳答案
如果你想用 COM 方式解决这个问题,你必须弄清楚在你的 VB 项目中添加哪个 COM 引用。
打开 regedit 并导航到 HKEY_CLASSES_ROOT\<class id>\CLSID
, IE。
HKEY_CLASSES_ROOT\Shell.Application\CLSID
您将找到唯一标识 COM 组件的类 ID。
在
HKEY_CLASSES_ROOT\CLSID
下您现在可以查找 COM 组件后面的文件:HKEY_CLASSES_ROOT\CLSID\{13709620-C279-11CE-A49E-444553540000}\InProcServer32
显示以下值:
%SystemRoot%\system32\SHELL32.dll
现在转到 Visual Studio,并添加对此文件的引用(在“添加引用”对话框的“浏览”选项卡上)。如果您打开项目属性,您实际上会看到添加的 COM 组件的好听的名称是 Microsoft Shell 控制和自动化 .
添加引用后,您可以使用
Shell.Application
对象如下:Option Strict On
Module PrintStartMenuLocation
Sub Main()
Dim shell As New Shell32.Shell
Dim folder As Shell32.Folder
Dim folderItem As Shell32.FolderItem
Dim startMenuPath As String
folder = shell.NameSpace(Shell32.ShellSpecialFolderConstants.ssfSTARTMENU)
folderItem = CType(folder.Items(0), Shell32.FolderItem)
startMenuPath = folderItem.Path
Console.WriteLine(startMenuPath)
End Sub
End Module
C# 中的版本如下所示:
class Program
{
static void Main(string[] args)
{
Shell32.Shell shell = new Shell32.Shell();
Shell32.Folder folder = shell.NameSpace(Shell32.ShellSpecialFolderConstants.ssfSTARTMENU);
Shell32.FolderItem folderItem = folder.Items().Item(0) as Shell32.FolderItem;
string startMenuPath = folderItem.Path;
Console.WriteLine(startMenuPath);
}
}
但是,如果您只需要检索开始菜单文件夹的位置,您可以直接在 .NET 中使用
Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)
关于vb.net - 在 COM 对象上使用早期绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/776331/