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!

About Author

Related Posts

Debian 12: linux-image-6.1.0-10amd64

Troubleshooting dependency issues in Debian 12: Resolving linux-image-6.1.0-10amd64 package dependency problems. If you installed the Debian 12 from the live image the issue is the raspi-firmware. Even…

C# Reference Types

Understanding C# Reference Types

One of the key features of C# is its support for C# reference types, which allow developers to create complex, object-oriented applications. In this blog post, we…

c# value types

Understanding C# Value Types

C# is a powerful programming language that is widely used for developing a wide range of applications. One of the key features of C# is its support…

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…

This Post Has One Comment

Leave a Reply