The code from this question works fine as below:
var scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));
dynamic obj = Activator.CreateInstance(scriptType, false);
obj.Language = "vbscript";
var vbscript = @"MsgBox(""Hello"");
obj.Eval(vbscript);
But as soon I try to add another line to the script, for example:
var vbscript = @"MsgBox(""Hello"")
MsgBox(""World"")";
I get a syntax error when calling obj.Eval(vbsbcript):
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Dynamic.dll
Additional information: Syntax error
I can see that the value of vbscript contains a load of \r\ns for the newlines:
"MsgBox(\"Hello\")\r\nMsgBox(\"World\")"
I tried declaring it with
vbscript = "MsgBox(\"Hello\")" +
Environment.NewLine +
"MsgBox(\"World\")";
but with the same results.
Is it possible to run some vbscript stored in a string which contains multiple lines in C#? If so how can this be achieved?
I know it is possible by calling a .vbs file with Process.Start(path), but can it be done directly from a string in the progam without having to depend on another file?