C# Code for BizTalk Application

C# Programming Language String in C# Language (Article) String in C# Language (Program)

239

Program:

using Microsoft.BizTalk.ExplorerOM;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

 

namespace ConsoleApplication3
{
    class BizTalkOperationClass
    {      

        static String[] lines = File.ReadAllLines(@"C:\Users\ansarir2\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\applicationsName.config", Encoding.UTF8);


         public static void stopAllApplications() { 
            // This method will help us to stop all the application present in Biztalk
             Console.WriteLine("______Application Stoping______\nPlease wait...");

             BtsCatalogExplorer catalog = new BtsCatalogExplorer();
             catalog.ConnectionString = "server=" + lines[0] + ";database=" + lines[1] + ";Integrated Security=SSPI";

            for (int i = 2; i < lines.Count(); i++)
            {
                // Console.WriteLine(lines[0]);
                if (lines[i] != null)
                {
                    Application app = catalog.Applications[lines[i]];
                    if (app != null)
                    {
                        Console.WriteLine(lines[i] + " Stopping...");
                        app.Stop(ApplicationStopOption.StopReferencedApplications);
                        app.Stop(ApplicationStopOption.UnenlistAllOrchestrations);
                        app.Stop(ApplicationStopOption.UnenlistAllSendPortGroups);
                        app.Stop(ApplicationStopOption.UnenlistAllSendPorts);
                        app.Stop(ApplicationStopOption.DisableAllReceiveLocations);
                        catalog.SaveChanges();
                    }
                    Console.WriteLine(i+1+": " +lines[i] + " Stopped");
                }                
            }
            Console.WriteLine("All Application Stopped");
            
        }



         public static void startAllApplications() 
         {
             Console.WriteLine("______Application Starting______\nPlease wait...");
             BtsCatalogExplorer catalog = new BtsCatalogExplorer();
             catalog.ConnectionString = "server=" + lines[0] + ";database=" + lines[1] + ";Integrated Security=SSPI";

            for (int i = 2; i < lines.Count(); i++)
             {
                 Console.WriteLine(lines[i]+"Starting...");
                 if(lines[i] != null)
                 {
                 Application app = catalog.Applications[lines[i]];
                 if (app != null)
                 {
                     app.Start(ApplicationStartOption.StartAll);
                     catalog.SaveChanges();
                 }
                 Console.WriteLine(i+1+": " + lines[i] + "Started");
                }
             }
             catalog.Refresh();
             Console.WriteLine("All BizTalk Applications are start now");
         }

        public static void undeploy()
        {
            Console.WriteLine("______Application Undeploying Started______\nPlease wait...");
            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = "server="+lines[0]+";database="+lines[1]+";Integrated Security=SSPI";
            
            for (int i = 2; i < lines.Count(); i++)
            {
                if (lines[i] != null)
                {
                    Application app = catalog.Applications[lines[i]];
                    if (app != null)
                    {
                        Console.WriteLine("hello "+i);
                        app.Stop(ApplicationStopOption.StopReferencedApplications);
                        app.Stop(ApplicationStopOption.UnenlistAllOrchestrations);
                        app.Stop(ApplicationStopOption.UnenlistAllSendPortGroups);
                        app.Stop(ApplicationStopOption.UnenlistAllSendPorts);
                        app.Stop(ApplicationStopOption.DisableAllReceiveLocations);
                        Thread.Sleep(8000);
                        catalog.SaveChanges();
                        
                        Process cmd = new Process();
                        cmd.StartInfo.FileName = "cmd.exe";
                        cmd.StartInfo.RedirectStandardInput = true;
                        cmd.StartInfo.RedirectStandardOutput = true;
                        cmd.StartInfo.CreateNoWindow = true;
                        cmd.StartInfo.UseShellExecute = false;
                        
                        cmd.Start();
                        // BTSTask RemoveApp /ApplicationName:DE.Unify.PlanManagement /Server=SLRWAPDVDAX87  /Database=DEV_BizTalkMgmtDb
                        cmd.StandardInput.WriteLine("BTSTask RemoveApp /ApplicationName:" + lines[i] + " /Server=SLRWAPDVDAX87  /Database=DEV_BizTalkMgmtDb");
                        // cmd.StandardInput.WriteLine("BTSTask UninstallApp /ApplicationName:" + lines[i] + "");
                        cmd.StandardInput.WriteLine("wmic product where 'description='" + lines[i] + "' ' uninstall");
                        cmd.StandardInput.WriteLine("for /d %a in (C:\\Windows\\Microsoft.NET\\assembly\\GAC_MSIL\\" + lines[i] + "*) do rd /q /s %a ");
                        cmd.StandardInput.Flush();
                        cmd.StandardInput.Close();
                        cmd.WaitForExit();
                        Console.WriteLine(cmd.StandardOutput.ReadToEnd());
                        Console.WriteLine("end");
                    }
                }

                Console.WriteLine("NOTE:- " + lines[i] + " Removed, Undeployed/Uninstalled, GAC file deleted.");
            }
        }

            public static void deploymentProcess(){
               
                String[] file = File.ReadAllLines(@"C:\Users\ansarir2\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\deployFile.config", Encoding.UTF8);
                String[] msiFiles = Directory.GetFiles("C:\\Users\\ansarir2\\Desktop\\installFolder", "*.msi");
                
                String[] msiFileNames = new String[msiFiles.Count()];
                Console.WriteLine(msiFiles.Count());
                Console.WriteLine("______Avialbale MSI______");
                for (int i = 0; i < msiFiles.Count(); i++)
                {
                    string p = Path.GetFileName(msiFiles[i]);
                    msiFileNames[i] = p;
                    Console.WriteLine(msiFileNames[i]);

                }
                Console.WriteLine("______Installation Started______");


                for (int i = 0; i < msiFileNames.Count(); i++)
                {
                    Process.Start(@"C:\Users\ansarir2\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\deployFile.config");
                    if (file[i] != null)
                    {
                        Process cmd = new Process();
                        cmd.StartInfo.FileName = "cmd.exe";
                        cmd.StartInfo.RedirectStandardInput = true;
                        cmd.StartInfo.RedirectStandardOutput = true;
                        cmd.StartInfo.CreateNoWindow = true;
                        cmd.StartInfo.UseShellExecute = false;
                        cmd.StartInfo.Verb = "runas";
                        cmd.Start();
                        // file[0] --> file Receive Location of msi
                        // file[1] --> Installation Location
                        // file[3] --> Host Instance
                        // msiFileNames[i] --> msi file names
                        cmd.StandardInput.WriteLine("cd "+file[0]);                        
                       // cmd.StandardInput.WriteLine();
                        cmd.StandardInput.WriteLine("msiexec /i " + msiFileNames[i] + " INSTALLDIR=\"" + file[1] + "\"");
                        cmd.StandardInput.Flush();
                        cmd.StandardInput.Close();
                        cmd.WaitForExit();
                        Console.WriteLine(cmd.StandardOutput.ReadToEnd());
                        Console.WriteLine("end");
                    }
                  
                }
                
            } 


            public static void exportMSI()
            {
                String[] file = File.ReadAllLines(@"C:\Users\ansarir2\Documents\Visual Studio 2013\Projects\ConsoleApplication3\ConsoleApplication3\deployFile.config", Encoding.UTF8);
                string man = "msiexec /i " + file[2] + " INSTALLDIR=\"" + file[0] + "\"";
                Thread.Sleep(2000);
                Console.WriteLine(man);
                Console.WriteLine("end delay");

            }

            public static void exportBindings()
            { 
                String[] msiFiles = Directory.GetFiles("C:\\Users\\ansarir2\\Desktop\\installFolder", "*.msi");
                String[] msiFileNames = new String[msiFiles.Count()];
                Console.WriteLine(msiFiles.Count());
                for (int i = 0; i < msiFiles.Count(); i++)
                {
                    string p = Path.GetFileName(msiFiles[i]);
                    msiFileNames[i] = p;
                    Console.WriteLine(msiFileNames[i]);

                }
                
            } 
    }
} 

Output:

Check in Visual Studio

Explanation:


using Microsoft.BizTalk.ExplorerOM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        { 

            int choice;
            int repeat = 1;
           
           

            while(repeat != 0){
                Console.Write("__________________________\n Stop Applications: 1 \n Start Application: 2 \n UnDeploy: 3 \n deploymentProcess: 4 \n Check: 5 \n__________________________\n");
                choice = int.Parse(Console.ReadLine());

            switch (choice) { 
                case 1:
                       BizTalkOperationClass.stopAllApplications();
                       break;
                case 2:
                       BizTalkOperationClass.startAllApplications();
                       break;                
                case 3:
                       BizTalkOperationClass.undeploy();
                       break;               
                case 4:
                       BizTalkOperationClass.deploymentProcess();
                       break;
                case 5:
                       BizTalkOperationClass.exportMSI();
                       break;
                case 6:
                       BizTalkOperationClass.exportBindings();
                       break;
                case 7:
                       BizTalkOperationClass.exportBindings();
                       break;
                default:
                       Console.WriteLine("Wrong Input");
                       break;

            }

            Console.Write("\nRepeat your choice: (1/0) ");
            repeat = int.Parse(Console.ReadLine());
        }  

            Console.ReadKey();
        }
    }
}



This Particular section is dedicated to Programs only. If you want learn more about C# Programming Language. Then you can visit below links to get more depth on this subject.