如何通过.NET执行Minitab命令?

xdu*_*ine 7 c# vb.net

Minitab帮助文件在有限的范围内提供有关此主题的支持,所有示例都在VB中.我是.NET的新手,但我很快就把它拿起来了.它在命令的语法中有所作为.

他们在VB中提供了这个例子:

Dim MtbApp As New mtb.Application
Dim MtbProj As mtb.Project
Dim MtbCom As mtb.Command
Dim i, j As Integer

MtbApp.UserInterface.Visible = True
Set MtbProj = MtbApp.ActiveProject
MtbProj.ExecuteCommand "RANDOM 30 C1 - C2"
MtbProj.ExecuteCommand "REGRESS C1 1 C2"
Run Code Online (Sandbox Code Playgroud)

我的代码在C#中看起来像这样

var MtbApp = new Mtb.Application();
var MtbProj = new Mtb.Project();
MtbProj = MtbApp.ActiveProject;
MtbApp.UserInterface.Visible = true;
MtbProj.ExecuteCommand(<command>);
Run Code Online (Sandbox Code Playgroud)

我期待应该发生的是Minitab应该打开,命令应该执行.但是,最新发生的是Minitab的两个实例正在打开而且都没有显示用户界面,我必须在进程中找到它们.

Mar*_*ark 12

假设您已添加对Minitab COM的引用,这应该可以帮助您入门:

Mtb.Application MtbApp = null;
Mtb.Project MtbProj = null;
Mtb.UserInterface MtbUI = null;

MtbApp = new Mtb.Application();
MtbProj = MtbApp.ActiveProject;
MtbUI = MtbApp.UserInterface;

MtbUI.Visible = true;
MtbProj.ExecuteCommand("RANDOM 30 C1-C2", Type.Missing); //with C# optional params required
MtbApp.Quit();

Marshal.ReleaseComObject(MtbUI); MtbUI = null;
Marshal.ReleaseComObject(MtbProj); MtbProj = null;
Marshal.ReleaseComObject(MtbApp); MtbApp = null;
Run Code Online (Sandbox Code Playgroud)

将C对象用于C#可能会非常棘手.特别是当你完成后释放它们.

请记住,作为一般规则永远不会加倍.不要这样做:

MtbApp.UserInterface.Visible = true;
Run Code Online (Sandbox Code Playgroud)

代替:

Mtb.UserInterface MtbUI = null;
MtbUI = MtbApp.UserInterface;
MtbUI.Visible = true;
Run Code Online (Sandbox Code Playgroud)

因此,MtbUI对象可以在以后释放.