Tuesday, October 22, 2013

C# code for removing a third party product from Dynamics GP

Problem
We have a Dynamics GP third party product that  has been decommissioned and needs to be removed. The manual process of removing a third party product is explained here:   https://mbs2.microsoft.com/Knowledgebase/KBDisplay.aspx?scid=kb;en-us;872087

However, I didn't want to place the burden on the customer/partner for removing it so I created a C# method that does this automatically.

Note: This code is called in a Console Application that's run by my installers. It's called in try catch block and errors are logged. This means errors are suppressed, but logged.


Code
 private static void RemoveProduct()
        {
            /*
             * Manual method for removing a third party product located here: https://mbs2.microsoft.com/Knowledgebase/KBDisplay.aspx?scid=kb;en-us;872087
             *
             * Check if the dic exists in the directory.
             *  If it does then
             *  1. Remove references to it from the Dynamics.set file
             *  2. Remove the file
             * 
             */
            string dic = "Product9999.DIC";
            string fFile = "F9999.DIC";
            string rFile = "R9999.DIC";
            string prodName = "My Product Name";
            string prodNum = "9999";

            string setPath = Path.Combine(Environment.CurrentDirectory, "Dynamics.set");
            string dicPath = Path.Combine(Environment.CurrentDirectory, dic);


            //Only execute if the dic is present
            if (File.Exists(dicPath))
            {

                //Only execute if the Dynamics.set file exists
                if (File.Exists(setPath))
                {
                    //use a string instead of the StringBuilder below, because otherwise we'd be removing from a StringBuilder and that's inefficient
                    string setFileString = "";
                    using (StreamReader reader = new StreamReader(setPath))
                    {
                        setFileString = reader.ReadToEnd();
                    }
                    if (!string.IsNullOrEmpty(setFileString))
                    {
                        StringBuilder sb = new StringBuilder(setFileString.Length);
                        string[] lines = setFileString.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                        //Decrement the first line by 1
                        sb = sb.AppendLine((int.Parse(lines[0]) - 1).ToString());

                        int removedCounter = 0;
                        for (int i = 1; i < lines.Length; i++)
                        {
                            //Once the 5 lines that specify a product have been removed we know we can stop looking,
                            //so append the rest of the lines

                            if (removedCounter < 5)
                            {
                                //If the line contains what we want removed, don't append to the StringBuilder
                                if (lines[i] == prodNum || lines[i] == prodName || lines[i].Contains(dic) || lines[i].Contains(fFile) || lines[i].Contains(rFile))
                                {
                                    removedCounter++;
                                    continue;
                                }
                            }
                            sb.AppendLine(lines[i]);
                        }


                        //Now write the StringBuilder to the Dynamics.set file
                        using (StreamWriter writer = new StreamWriter(setPath, false))
                        {
                            writer.Write(sb.ToString());
                        }


                    }
                }
                //Now delete the dic file
                File.Delete(dicPath);
            }
        }

No comments:

Post a Comment

There was an error in this gadget