C# – Add VB script capabilities to your applications

ms-visual-c-icon

There 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.

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();
    }
  }
}

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

x=msgbox("Welcome to this script" ,0, "Welcome")

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!

Related Posts

C# check if server is online

C# check if server is online directly from your code. Check servers or services like web servers, database servers like MySQL and MongoDB. You can probably check…

C# Convert Int to Char

C# convert int to char google search is giving some result which I think is not directly what some people want. Most results are giving instructions on…

c# bash script

C# Bash Script Made Easy

There are many reasons why it could be handy to run a bash script from a C# application. In 2014, before I changed to a Mac as…

go hello world

Golang Hello World, Get a Easy Fantastic Start

Golang hello world example tutorial. I assume you are new to the golang language since you found this website. Go is one of the latest programming languages…

Unity persistant datapath

Unity Persistent Data Path

The Unity persistentDataPath is read only and will return the path where you can save data that will be persitent betwen sessions. Even app updates doesn’t touch…

csharp ping check

C# Ping Check the easy way

How can we check if a computer, server, or another device is online? We can use a C# Ping check. If you can check the device with…

This Post Has One Comment

Leave a Reply