The WScript
对象特定于 Windows 脚本宿主,在 .NET Framework 中不存在。
事实上,所有的WScript.Shell
.NET Framework 类中提供了对象功能。因此,如果要将 VBScript 代码移植到 VB.NET,则应该使用 .NET 类重写它,而不是使用 Windows Script Host COM 对象。
如果由于某种原因您更喜欢使用 COM 对象,则需要将适当的 COM 库引用添加到您的项目中,以便使这些对象可供您的应用程序使用。的情况下WScript.Shell
, it's %WinDir%\System32\wshom.ocx (or %WinDir%\SysWOW64\wshom.ocx在 64 位 Windows 上)。然后你可以编写这样的代码:
Imports IWshRuntimeLibrary
....
Dim shell As WshShell = New WshShell
MsgBox(shell.ExpandEnvironmentStrings("%windir%"))
或者,您可以使用以下命令创建 COM 对象的实例
Activator.CreateInstance(Type.GetTypeFromProgID(ProgID))
and then work with them using late binding. Like this, for example*:
Imports System.Reflection
Imports System.Runtime.InteropServices
...
Dim shell As Object = Nothing
Dim wshtype As Type = Type.GetTypeFromProgID("WScript.Shell")
If Not wshtype Is Nothing Then
shell = Activator.CreateInstance(wshtype)
End If
If Not shell Is Nothing Then
Dim str As String = CStr(wshtype.InvokeMember(
"ExpandEnvironmentStrings",
BindingFlags.InvokeMethod,
Nothing,
shell,
{"%windir%"}
))
MsgBox(str)
' Do something else
Marshal.ReleaseComObject(shell)
End If
* I don't know VB.NET well, so this code may be ugly; feel free to improve.