Kindly help.

I want to evalaute a mathematica notebook using C#. I have made a mathematica notebook named 'pureimpure.nb'. I like to evaluate it from dot net using the command


(not sure if command is right one).

But it is not executing. Everytime it is showing the command itself as the result. Please correct me or please advice me with the right set of commands.

Thank You

  • 2
    $\begingroup$ At least one problem is that the file name is not quoted. I suggest you try the command in a Mathematica session before you attempt to evaluate it through C#. $\endgroup$
    – Szabolcs
    Commented Jan 9, 2020 at 12:54
  • $\begingroup$ This is probably quite impossible. What you can do is evaluate kernel expressions from C# and even that is cumbersome. Check out the NETLink Tutorial and the examples in SystemOpen @FileNameJoin[{$InstallationDirectory, "SystemFiles", "Links", "NETLink", "Examples", "Part2"}] $\endgroup$ Commented Jan 9, 2020 at 14:15
  • $\begingroup$ Too busy to answer, but here are some pointers: (1) mathematica.stackexchange.com/a/96493/363 (2) mathematica.stackexchange.com/q/115331/363 (3) stackoverflow.com/q/7626491/879601 $\endgroup$ Commented Jan 9, 2020 at 14:26

1 Answer 1


The code below creates a form which opens and evaluates my notebook ok.nb. You will have to figure out how to close the front end while still giving your notebook enough time to evaluate. This demo pauses 5 seconds. There is a more sophisticated option here.

You may be able to omit the CreateKernel() method and simply call kernel = new MathKernel(), but that does not work on my setup (which includes multiple Mathematica versions);

using System;
using System.Windows.Forms;
using Wolfram.NETLink;

namespace RunMathematicaNotebook
    public partial class Form1 : Form
        public Form1()

        private void Button1_Click(object sender, EventArgs e)
            MathKernel kernel = null;
                kernel = CreateKernel();
                kernel.Compute("UsingFrontEnd[nb = NotebookOpen[\"C:\\Users\\chrisd\\Documents\\ok.nb\"]; NotebookEvaluate[nb]]");
                kernel.Compute("Pause[5]; CloseFrontEnd[]");

                MessageBox.Show("Error connecting to Mathematica", "Mathematica Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                if (kernel != null)

        public MathKernel CreateKernel()
            string mlArgs = "-linkmode launch -linkname \"C:\\\\Program Files\\\\Wolfram Research\\\\Mathematica\\\\11.2\\\\MathKernel.exe\"";

            IKernelLink ml = MathLinkFactory.CreateKernelLink(mlArgs);

            MathKernel mathKernel = new MathKernel
                AutoCloseLink = true,
                CaptureGraphics = true,
                CaptureMessages = true,
                CapturePrint = true,
                GraphicsFormat = "Metafile",
                GraphicsHeight = 0,
                GraphicsResolution = 80,
                GraphicsWidth = 0,
                HandleEvents = true,
                Input = null,
                Link = ml,
                LinkArguments = null,
                PageWidth = 0,
                ResultFormat = MathKernel.ResultFormatType.OutputForm,
                UseFrontEnd = true
            return mathKernel;

As well as adding a reference to Wolfram.NETLink you will probably need to copy ml32i4.dll to your project folder and make sure it copies to your output directory. There is some more info about that here.

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.