El objeto WScript
es específico de Windows Script Host y no existe en .NET Framework.
En realidad, todas las funciones del objeto WScript.Shell
están disponibles en las clases de .NET Framework. Por lo tanto, si transfiere el código de VBScript a VB.NET, debe volver a escribirlo usando clases .NET en lugar de usar los objetos COM de Windows Script Host.
Si, por alguna razón, prefiere utilizar objetos COM de todos modos, debe agregar las referencias apropiadas de la biblioteca COM a su proyecto para que estos objetos estén disponibles para su aplicación. En el caso de WScript.Shell
, es % WinDir% \ System32 \ wshom.ocx (o % WinDir% \ SysWOW64 \ wshom.ocx en Windows de 64 bits). A continuación, puede escribir código como este:
Imports IWshRuntimeLibrary
....
Dim shell As WshShell = New WshShell
MsgBox(shell.ExpandEnvironmentStrings("%windir%"))
Como alternativa, puede crear instancias de objetos COM utilizando
Activator.CreateInstance(Type.GetTypeFromProgID(ProgID))
y luego trabajar con ellos utilizando el enlace en tiempo. Así, por ejemplo *:
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
* No sé VB.NET así, por lo que este código puede ser feo; siéntase libre de mejorar.
Revisa el Blog de Eric Lippert http://blogs.msdn.com/b/ericlippert/archive/2003/10/08/53175.aspx – volody
@volody Esa es una publicación interesante, pero aún me gustaría saber cómo adaptarla mi código para vb.net – user1196604
puede ejecutar script como proceso separado – volody