绑定到现有进程

绑定到现有进程

问题描述:

我试图打开SQL Server Management Studio中,programitically,我已经使用过程打开SQL Server和已经使用start()方法打开的过程中,绑定到现有进程

    using System.Diagnostics; 
        Process Sql = new Process(); 
        string strfile1="example1.sql"; 
        string strfile2="example2.sql"; 
        Sql.StartInfo.FileName = "Ssms.exe";//sql server process 
        Sql.StartInfo.Arguments = strfile1; 
        Sql.Start(); 
        Sql.StartInfo.Arguments = strfile2; 
        Sql.Start(); 

此代码打开SQL Server的两个实例,但我想检查该进程是否已在运行,并重用现有进程并在同一Process中打开example2.sql。可以这样做吗?

如果一个进程的名字运行您可以检查(你需要找出你的SSMS进程的名字)是这样的:

Process[] procName= Process.GetProcessesByName("INSERT NAME HERE"); 
if (procName.Length >= 0) 
{ 
    //you are already running 
} 

,但我不认为你可以再修改ssms的现有实例。

如果让操作系统决定要做什么,该怎么办?

Process.Start("example1.sql"); 
Process.Start("example2.sql");