3

I have a VBScript that's working fine. I have a C# program that can run the VBScript with the cscript program.

What I want to do is embed the VBS in a C# exe so it is one single file and be able to run the VBS file. Is there any way for me to pass the embedded VBScript file to cscript as an argument or some other way to run an embedded VBS file?

2

2 Answers 2

7
Type scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));

dynamic obj = Activator.CreateInstance(scriptType, false);
obj.Language = "vbscript";
string vbscript = "msgbox(\"test\")";
obj.Eval(vbscript);
Sign up to request clarification or add additional context in comments.

5 Comments

working example of a WinForms app that does this: cheeso.members.winisp.net/…
How am I supposed to format the syntax of the VBScript to make it work? The msgbox example works but I'm getting syntax error when trying to run my script with VBScript reserve words.
@L.B This doesn't work: Wscript.Echo("hello"). Any idea why?
@DanW The answer to your question (11 years late) is that WScript.Echo is only available when the script is run via the WScript.exe or CScript.exe host. This also applies to other WScript commands, such as WScript.Sleep.
For future reference as this answer still comes up when googling for this: To get proper types (and Autocompletion) in VisualStudio right click on Dependencies -> Add COM Reference -> Browse -> "C:\Windows\SysWOW64\msscript.ocx" -> OK. Then you can cast the result of Activator.CreateInstance(scriptType, false) to MSScriptControl.ScriptControl. Also note that the script control only exists in 32 bit version, so set Platfrom to x86 in Configuration Manager. - @L.B Feel free to add this to your answer.
3

Another approach is that you can execute CSCRIPT.EXE via a sub process:

Process.Start("cscript.exe", "C:\\myscript.vbs");

However, keep in mind that .NET and C# is very compatible with VBS scripting. You might want to just rewrite the VBS code as a C# program directly.

6 Comments

How would that work with an embedded resource if there's no path? I'm using Assembly.GetExecutingAssembly() .GetManifestResourceStream("script.vbs"))
extract your resource to a Temp File, execute CScript with the Temp File. Then delete the Temp file?
@PaulFarry Well obviously I could do that but I'm trying to find a way that does not expose the file.
@Jack sorry, but it wasn't obvious. you didn't state it in your question or comment that this was something you tested.
@PaulFarry I already mentioned that I could run the file from the C# program. I said "What I want to do is embed the VBS in a C# exe so it is one single file and be able to run the VBS file." and "some other way to run an embedded VBS file?"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.