Hi @Deepak Rane (drane) , Welcome to Microsoft Q&A,
Use Type.GetTypeFromProgID("MSScriptControl.ScriptControl") to dynamically load COM types. Set the Language property to "JScript" to use the JScript engine. Use the Eval method to directly run the script and get the return value.
using System;
namespace xxx
{
internal class Program
{
static void Main(string[] args)
{
try
{
// Load the COM object of the JScript engine
Type scriptControlType = Type.GetTypeFromProgID("MSScriptControl.ScriptControl");
if (scriptControlType == null)
{
Console.WriteLine("Unable to load MSScriptControl.ScriptControl. Make sure the correct component is registered and installed.");
return;
}
dynamic scriptControl = Activator.CreateInstance(scriptControlType);
// Set the language to JScript
scriptControl.Language = "JScript";
// Define and execute the script
string script = @"
function add(a, b) {
return a + b;
}
add(10, 20);
";
object result = scriptControl.Eval(script);
Console.WriteLine($"Script execution result: {result}");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred: {ex.Message}");
}
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.