How can I execute VBScript in C#

15,780

Solution 1

finally, we decide to migrate to IronPython instead of VBS, vbs is not supported by microsoft. so it's time for move on

Solution 2

You can execute VBScript from C# code.

Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"C:\text.vbs";
scriptProc.Start();
scriptProc.WaitForExit();
scriptProc.Close();

There are some important things that you have to consider. Take a look at this answer

Share:
15,780
Sean
Author by

Sean

full stack programmer, like coding, like new stuff, like to share

Updated on June 04, 2022

Comments

  • Sean
    Sean about 2 years

    We have a legacy system that allow customer to custmize the workflow by vbscript, for example, the user could write some code like

    for each account in accounts {
       Dim acc as Integer
       .....
    }
    

    the code inside the {...} is VBScript. and the legacy system is writtern by VB, and use ScriptControl to execute the script directly. Now we are going to upgrade the system to C# .NET 4.5, we want to get rid of the old scriptcontrol but want to keep the scripts from users.

    My question is it possible to execute vb script from C#?

    Thanks

  • Sean
    Sean about 11 years
    thanks for your reply, is it possible to process a string instead of a test.vbs ? because the script of our legcay system is not just a vbs, we have our self domain specific language, as the code I showed, the vbs is inside the {...} so I need to reader the file get the script inside the {...} and execute it
  • diyoda_
    diyoda_ about 11 years
    can't you put the VBScript code in separate files and execute as you wish?
  • Sean
    Sean about 11 years
    I wish I could, as a legacy system that exists for 10 years with a large amout of these old scripts file, my boss may kill me if I ask to separate these files, the difficute part for a upgrade project is we must keep customer happy and change the script they are using will not make them happy :)
  • diyoda_
    diyoda_ about 11 years
    In my opinion, if you are going to upgrade upgrade the legacy system then why worry about this. Normally during these upgrades, we are not touching the legacy system, what we do is only interface with it and input out put relevant data and trigger the system using the interface from the new code.You have to get some from experts on this after explaining the situation a bit more. Try C# Chat.
  • peter
    peter about 11 years
    You don't need to extract and separate all the scripts, but when you need one you save the contents of what is between {...} and save this to a temporary script and execute it.