public static void Main(string[] args)
        {
            Boolean syncParam   = false;
            Boolean deleteParam = false;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].ToLower().Equals("-sync"))
                {
                    syncParam = true;
                }
                else if (args[i].ToLower().Equals("-delete"))
                {
                    deleteParam = true;
                }
            }

            Configuration         config    = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            AppSettingsSection    amSection = (AppSettingsSection)config.Sections["AMSettings"];
            ActiveDirectoryClient client    = new ActiveDirectoryClient(config);

            client.getUserDetailsFromActiveDirectory(
                amSection.Settings["UserExcludeFilter"].Value,
                amSection.Settings["ExcludeDisableAccount"].Value
                );
            string[] csvStr = client.createImportFile();
            Console.WriteLine("################### Sync Users - Start ###################");
            Console.WriteLine("");
            StringBuilder importDataStr = new StringBuilder();

            for (int i = 0; i < csvStr.Length; i++)
            {
                importDataStr.AppendLine(csvStr[i]);
                Console.WriteLine(csvStr[i]);
            }
            string response = "";

            if (syncParam)
            {
                response = client.uploadToAlertMedia(importDataStr.ToString());
                Console.WriteLine("*********************************************************");
                Console.WriteLine("");
                Console.WriteLine(response);
                Console.WriteLine("");
                Console.WriteLine("*********************************************************");
            }
            else
            {
                Console.WriteLine("*********************************************************");
                Console.WriteLine("");
                Console.WriteLine(" Above listed users have not been synced to AlertMedia. This was a dry run. To actually sync them, you need to send an args parameter (-sync)");
                Console.WriteLine("");
                Console.WriteLine("*********************************************************");
            }
            Console.WriteLine("");
            Console.WriteLine("################### Sync Users - End ###################");
            if (syncParam)
            {
                ArrayList                   importedUserList = new ArrayList();
                JavaScriptSerializer        j = new JavaScriptSerializer();
                Dictionary <string, object> a = (Dictionary <string, object>)j.Deserialize(response, typeof(Dictionary <string, object>));
                StringBuilder               textBoxResponse = new StringBuilder();
                if (a.ContainsKey("stats"))
                {
                    importedUserList = (ArrayList)a["successes"];
                    ArrayList failuresList = (ArrayList)a["failures"];
                    for (int i = 0; i < failuresList.Count; i++)
                    {
                        importedUserList.Add(failuresList[i]);
                    }
                }
                Console.WriteLine("################### Delete Users - Start ###################");
                Console.WriteLine("");
                string[]  delCsvStr         = client.previewDeleteUserList(importedUserList);
                ArrayList deletedUserIdList = new ArrayList();
                for (int i = 1; i < delCsvStr.Length; i++)
                {
                    string strLine = delCsvStr[i];
                    Console.WriteLine(strLine);
                    List <string> fields = SplitCSV(strLine);
                    deletedUserIdList.Add(fields[0].Replace("\"", ""));
                }
                if (deleteParam)
                {
                    client.deleteUsersFromAlertMedia(deletedUserIdList);
                    Console.WriteLine("*********************************************************");
                    Console.WriteLine("");
                    Console.WriteLine(" All the " + deletedUserIdList.Count + " users have been deleted from AlertMedia ");
                    Console.WriteLine("");
                    Console.WriteLine("*********************************************************");
                }
                else
                {
                    Console.WriteLine("*********************************************************");
                    Console.WriteLine("");
                    Console.WriteLine(deletedUserIdList.Count + " of the above listed users have not been deleted from AlertMedia. This was a dry run. To actually delete them, you need to send two args parameter (-sync -delete)");
                    Console.WriteLine("");
                    Console.WriteLine("*********************************************************");
                }
                Console.WriteLine("");
                Console.WriteLine("################### Sync Users - End ###################");
                Console.ReadLine();
            }
        }
        private void previewButton_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            amSection.Settings["UserExcludeFilter"].Value = filterField.Text;
            if (disableAccounts.Checked)
            {
                amSection.Settings["ExcludeDisableAccount"].Value = "yes";
            }
            else
            {
                amSection.Settings["ExcludeDisableAccount"].Value = "no";
            }
            config.Save(ConfigurationSaveMode.Minimal);
            syncButton.Enabled     = false;
            downloadButton.Enabled = false;
            resultsPanel.Text      = "Results of syncing users from Active Directory to AlertMedia will be displayed here";
            this.Controls.Remove(tablePanel);
            this.Controls.Add(tablePanel);
            StringBuilder dataStr = new StringBuilder();

            string[] csvStr;
            try
            {
                client = new ActiveDirectoryClient(this.config);
                client.getUserDetailsFromActiveDirectory(filterField.Text, amSection.Settings["ExcludeDisableAccount"].Value);
                csvStr = client.createImportFile();
                string   htmlContent = "<html><body style=\"padding: 0; margin: 0;\"><table>";
                string[] headers     = this.SplitCSV(csvStr[0]);
                htmlContent = htmlContent + "<tr>";
                dataStr.AppendLine(csvStr[0]);
                for (int i = 0; i < headers.Length; i++)
                {
                    string value = headers[i].Replace("\"", "");
                    if (value == "")
                    {
                        value = "&nbsp;";
                    }
                    htmlContent = htmlContent + "<th style=\"font-family: sans-serif; font-size: 12px; background: #f2e6d9; border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; padding: 0cm 0cm 0cm 0cm; height: 10.75pt;\" ><b>" + value + "</b></th>";
                }
                htmlContent = htmlContent + "</tr>";
                for (int i = 1; i < csvStr.Length; i++)
                {
                    string strLine = csvStr[i];
                    dataStr.AppendLine(strLine);
                    string[] fields = this.SplitCSV(strLine);
                    htmlContent = htmlContent + "<tr>";
                    for (int j = 0; j < fields.Length; j++)
                    {
                        string value = fields[j].Replace("\"", "");
                        if (value == "")
                        {
                            value = "&nbsp;";
                        }
                        htmlContent = htmlContent + "<td style=\"font-family: sans-serif; font-size: 10px; border: solid #DEDEDE 1.0pt; mso-border-alt: solid #DEDEDE .75pt; padding: 0cm 0cm 0cm 0cm; height: 10.75pt;\" >" + value + "</td>";
                    }
                    htmlContent = htmlContent + "</tr>";
                }
                htmlContent             = htmlContent + "</table></body></html>";
                tablePanel.DocumentText = htmlContent;


                importCSVData          = dataStr.ToString();
                syncButton.Enabled     = true;
                downloadButton.Enabled = true;
                Cursor.Current         = Cursors.Default;
            }
            catch (Exception ex)
            {
                tablePanel.DocumentText = "";
                using (StreamWriter w = File.AppendText("adsynclog.txt"))
                {
                    Utils.Log(ex.ToString(), w);
                }

                MessageBox.Show("Not able to connect to you Active Directory instance. Please check you internet connection and try again. If problem persists, please contact your internal support team.");
                Cursor.Current = Cursors.Default;
                return;
            }
        }