Click here to Skip to main content
15,885,146 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
i have vb scripts , how can i execute these vb scripts in my .net application.

hai i am new to .net and vb scripts , its an urgent requirement so please help me.
thanks in advance.
Posted
Updated 12-Sep-14 16:58pm
v2

You can use:
C#
System.Diagnostics.Process.Start(@"cscript //B //Nologo c:\<path>\myScriptFile.vbs");
</path>


Please keep in mind that you will have limited interaction with the script from your application.
 
Share this answer
 
Comments
sateesh kumar nemalikanti 12-Sep-14 10:08am    
thanks nathan,

i use below code
Try
Dim start As New ProcessStartInfo
start.FileName = "C:\WINDOWS\system32\cscript.exe"
start.Arguments = "C:\ProgramFiles\ActiveXperts\NetworkMonitor\Scripts\Monitor(vbs)\Cpu.Vbs"
start.UseShellExecute = False
start.RedirectStandardOutput = True
start.RedirectStandardError = True
Dim myproc As New Process
myproc.StartInfo = start
myproc.Start()
Dim so As System.IO.StreamReader
Dim se As System.IO.StreamReader
se = myproc.StandardError
so = myproc.StandardOutput
myproc.WaitForExit()
MsgBox(so.ReadToEnd)
MsgBox(se.ReadToEnd)
Catch ex As Exception

End Try
but i getting error like script file not found with the preifx like this i was getting
Nathan Minier 12-Sep-14 10:23am    
According to CPallini's link below you don't need to execute off of cscript, which makes my answer less than ideal. Change start.FileName = "C:\ProgramFiles\ActiveXperts\NetworkMonitor\Scripts\Monitor(vbs)\Cpu.Vbs"

and remove start.Arguments based on that.
Sergey Alexandrovich Kryukov 12-Sep-14 23:15pm    
You are right. A comprehensive way of using WSH would be linking to the Windows Host Script Model library, which you can always do in .NET, because this is just the OCX module. Please see my answer.
—SA
See, for instance here[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Sep-14 23:16pm    
As Nathan correctly put it in Solution 1, running an external process for such purposes won't be very good. A comprehensive way of using WSH would be linking to the Windows Host Script Model library, which you can always do in .NET, because this is just the OCX module. Please see my answer.
—SA
If you want to use WSH seriously, don't run the script as a separate process. As Solution 1 correctly explained, your interaction with that process would be limited. Instead, link Windows Host Script Model in your application.

To do so, add a reference to your project. Use the tab "COM" of the "Add Reference Window", choose, "Windows Host Script Model" (wshom.ocx). The wrapper with the interface will be generated for your project C# project. You will need to use the namespace IWshRuntimeLibrary. Follow the Windows Script Host documentation.

—SA
 
Share this answer
 
Comments
sateesh kumar nemalikanti 13-Sep-14 1:11am    
Thanks SA can you please explain some more detail with the help of my above attached code..... please
sateesh kumar nemalikanti 13-Sep-14 1:40am    
as your said i was doing like below but not getting SA, please make me clear .its urgent requirement please help me . thanks
Imports System.IO
Imports IWshRuntimeLibrary
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim p = Process.Start("D:\Cpu.Vbs")
' p.WaitForExit()
Dim so As System.IO.StreamReader
Dim se As System.IO.StreamReader
se = p.StandardError
so = p.StandardOutput
p.WaitForExit()
MsgBox(so.ReadToEnd)
MsgBox(se.ReadToEnd)
End Sub
End Class
Sergey Alexandrovich Kryukov 13-Sep-14 19:41pm    
This code has nothing to do with my solution. I said: don't start the process, embed WSH...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900