Category: VB Script

May 3rd, 2014 by ronny

ms-visual-c-iconThere is plenty of areas to use this. For me, I use vb scripts on one of my software so users can make their own start up and shutdown scripts. And here is how to implement it into your application.

[sourcecode language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace cs_script1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Script info initializing…");
Process vbScript = new Process();
vbScript.StartInfo.FileName = @"cscript";
vbScript.StartInfo.WorkingDirectory = @"c:\scripts\";
vbScript.StartInfo.Arguments = "hello.vbs";
vbScript.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
vbScript.Start();
Console.WriteLine("Script started…..");
vbScript.WaitForExit(); // Remove if you don’t want the script to finish before continue
vbScript.Close();
Console.WriteLine("Script executed….");
Console.ReadLine();
}
}
}
[/sourcecode]

First we use the system.diagnostic to create a new process.
With the startInfo.FileName we type in the Windows Script Host (cscript).
Next line is just the working directory, where we work with our script files.
The script itself will be an argument for the script host.
WaitForExit, this one make sure the script is done before continuing with the c# application. If it is not needed for the application to wait for the script to exit, just remove that part.

Example hello.vbs

[sourcecode language=”vb”]
x=msgbox("Welcome to this script" ,0, "Welcome")
[/sourcecode]

You can start as many instances of the script as you like. Just run the vbScript.Start() several times to start it more than once.

And that’s it really.

Happy scripting!

Posted in C#, Programming, Uncategorized, VB Script

Optimization WordPress Plugins & Solutions by W3 EDGE