Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a VBScript file in a C# application?

I need to call a VBScript file (.vbs file extension) in my C# Windows application. How can I do this?

There is an add-in to access a VBScript file in Visual Studio. But I need to access the script in code behind. How to do this?

like image 961
balaweblog Avatar asked Oct 14 '08 08:10

balaweblog


People also ask

How do I view a VBScript file?

Open Task Manager and go to Details tab. If a VBScript is running, the process wscript.exe or cscript.exe would appear in the list. Right-click on the column header and enable "Command Line". This should tell you which script file is being executed.

Is VBS file executable?

There is no way to convert a VBScript (. vbs file) into an executable (.exe file) because VBScript is not a compiled language. The process of converting source code into native executable code is called "compilation", and it's not supported by scripting languages like VBScript.


1 Answers

The following code will execute a VBScript script with no prompts or errors and no shell logo.

System.Diagnostics.Process.Start(@"cscript //B //Nologo c:\scripts\vbscript.vbs"); 

A more complex technique would be to use:

Process scriptProc = new Process(); scriptProc.StartInfo.FileName = @"cscript";  scriptProc.StartInfo.WorkingDirectory = @"c:\scripts\"; //<---very important  scriptProc.StartInfo.Arguments ="//B //Nologo vbscript.vbs"; scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //prevent console window from popping up scriptProc.Start(); scriptProc.WaitForExit(); // <-- Optional if you want program running until your script exit scriptProc.Close(); 

Using the StartInfo properties will give you quite granular access to the process settings.

You need to use Windows Script Host if you want windows, etc. to be displayed by the script program. You could also try just executing cscript directly but on some systems it will just launch the editor :)

like image 71
Ilya Kochetov Avatar answered Sep 28 '22 13:09

Ilya Kochetov