views:

9568

answers:

5

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?

+14  A: 

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.Arguments ="//B //Nologo c:\scripts\vbscript.vbs";
scriptProc.Start();
scriptProc.WaitForExit();
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 :)

Ilya Kochetov
+1  A: 

You mean you try to run a vbs file from C#?

It can be done like running any other program from C# code:

Process.Start(path);

But you have to make sure that it won't ask for anything, and it is running with the command line version of the interpreter:

Process.Start("cscript path\\to\\script.vbs");
Biri
A: 

Are you developing an ASP.NET web application for Internet Explorer only and want to execute VBScript on the client or would you like to execute it on the server...?

On the client I think you need to work with ClientScript.RegisterStartupScript method. On the server I'd (carefully) do it als Ilya described in his answer.

Mudu
A: 

How would you get back results from the VBS?

Don't post questions as answers to other questsions. Use the **Ask Questsion** button in the top right to post new questsions.
Helen
A: 

C:\Documents and Settings\All Users.WIN\Desktop\obituaryprogram.com\TheWord\theword.exe

I have a bible stored in the above directory and would like to open it from a link on my website. It's currently asking the viewer if they would like to run or save this. I'd like for it to automatically open when the link is clicked on. I built the site using WebPlus10x2. I tried adding a html attachment and embedding. Can you please tell me how to accomplish opening without running? Thank you. Brenda

Brenda Dees
This should be asked as a separate question.
Duncan Bayne