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

90,346

Solution 1

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 :)

Solution 2

Another approach is to create a VB.NET Class Library project, copy your VBScript code into a VB.NET Class file, and reference the VB.NET Class Library from your C# program.

You will need to fix-up any differences between VBScript and VB.NET (should be few).

The advantage here, is that you will run the code in-process.

Solution 3

This is a permissions issue. Your application appPool must be running at the highest permission level to do this in 2008. The Identity must be Administrator.

Solution 4

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");

Solution 5

For the benefit of searchers, I found this post, which gives a clear answer (esp if you have parameters). Have tested it - seems to work fine.

string scriptName = "myScript.vbs"; // full path to script
int abc = 2;
string name = "Serrgggio";

ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "cscript.exe";
ps.Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\"", scriptName, abc, name);
//This will equate to running via the command line:
// > cscript.exe "myScript.vbs" "2" "Serrgggio"
Process.Start(ps);
Share:
90,346
balaweblog
Author by

balaweblog

hi this is Balamurugan I am active member of this stackoverflow site. Please visit my blog http://balaweblog.wordpress.com

Updated on September 28, 2020

Comments

  • balaweblog
    balaweblog over 3 years

    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?

  • roydukkey
    roydukkey about 13 years
    Will the program calling System.Diagnostics.Process.Start() hang until the process ends, or will it continue on leaving the process to run in the background?
  • roydukkey
    roydukkey about 13 years
    Yes. By default the program will run without waiting for the process to end. However, System.Diagnostics.Process.Start().WaitForExit() will force the calling program to wait for the process to end.
  • qxotk
    qxotk about 13 years
    However, I did use the method from @Ilya above - this was an excellent way for me to test that my vbscript and vb.net code was producing the same results.
  • Spock
    Spock over 6 years
    I think the filename has to be the file name. i.e not cscript
  • Tathagat Verma
    Tathagat Verma about 6 years
    Can I pragmatically add "permissions to execute" as well?