private void ExtractFtIcon(string productFileName, RemoteAppLib.New.FileTypeAssociation fta)
 {
     // Extract icon for filetype
     if (!IconModule.ExtractToIco(fta.IconPath, int.Parse(fta.IconIndex), productFileName + "." + fta.Extension + ".ico"))
     {
         // If filetype icon fails to extract, then grab the default document icon from Shell32.dll
         IconModule.ExtractToIco(RemoteAppFunction.GetSysDir() + @"\shell32.dll", 0, productFileName + "." + fta.Extension + ".ico");
         // Possibly show an error here??
     }
 }
        private void CreateButton_Click(object sender, EventArgs e)
        {
            var MSIPath = string.Empty;

            if (CheckBoxSignRDPEnabled.Checked && string.IsNullOrEmpty(CertificateComboBox.SelectedItem as string))
            {
                MessageBox.Show("You must select a certificate to sign the RDP file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (DisabledFTACheckBox.Checked && _remoteApp.FileTypeAssociations != null)
            {
                _remoteApp.FileTypeAssociations.Clear();
            }
            string RDPPath;

            if (RDPRadioButton.Checked)
            {
                FileSaveRDP.FileName = _remoteApp.Name;
                if (FileSaveRDP.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                RDPPath = FileSaveRDP.FileName;
            }
            else
            {
                FileSaveMSI.FileName = _remoteApp.Name;
                if (FileSaveMSI.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                MSIPath = FileSaveMSI.FileName;
                RDPPath = Environment.GetEnvironmentVariable("TEMP") + @"\" + _remoteApp.Name + ".rdp";
                _       = Environment.GetEnvironmentVariable("TEMP") + @"\" + _remoteApp.Name + ".msi";
            }

            if (UseRDGatewayCheckBox.Checked)
            {
                _ = GatewayAddress.Text;
                _ = AttemptDirectCheckBox.Checked;
            }

            if (RDPRadioButton.Checked)
            {
                CreateRDPFile(RDPPath, _remoteApp);
                // !!!!!!! If it's an RDP file
                if (EditAfterSave.Checked)
                {
                    var CmdLine = RemoteAppFunction.GetSysDir() + @"\notepad.exe";
                    Process.Start(CmdLine, FileSaveRDP.FileName);
                }

                if (CreateRAWebIcon.Checked)
                {
                    var IconFilePath = RDPPath.Substring(0, RDPPath.Length - 4) + ".ico";
                    if (!IconModule.ExtractToIco(_remoteApp.IconPath, _remoteApp.IconIndex, IconFilePath))
                    {
                        MessageBox.Show("Icon could not be created the RemoteApp. RDP file will still be created.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    // Check if there are file type associations before trying to work with the file type association icons
                    if (_remoteApp.FileTypeAssociations != null)
                    {
                        foreach (RemoteAppLib.New.FileTypeAssociation fta in _remoteApp.FileTypeAssociations)
                        {
                            var ProductFileName = RDPPath.Substring(0, RDPPath.Length - 4);
                            ExtractFtIcon(ProductFileName, fta);
                        }
                    }
                }

                Close();
            }
            else
            {
                // !!!!!!!  If it's an MSI
                var RDP = new RDP2MSIlib.New.RDP();
                CreateRDPFile(RDPPath, _remoteApp);
                RDP.RDPFilePath              = RDPPath;
                RDP.ShortcutOnDesktop        = ShortcutDesktopCheckBox.Checked;
                RDP.ShortcutInStart          = ShortcutStartCheckBox.Checked;
                RDP.ShortcutSubfolderInStart = SubfolderRadioButton.Checked;
                RDP.ProductRemoteTag         = ShortcutTagTextBox.Text;
                RDP.PerUser = PerUserRadioButton.Checked;
                var FilesToDelete   = new List <string>();
                var ProductFileName = RDPPath.Substring(0, RDPPath.Length - 4);
                var IconFilePath    = ProductFileName + ".ico";
                FilesToDelete.Add(RDPPath);
                if (!IconModule.ExtractToIco(_remoteApp.IconPath, _remoteApp.IconIndex, IconFilePath))
                {
                    MessageBox.Show("There was an error loading icon:" + Environment.NewLine + _remoteApp.IconPath + "," + _remoteApp.IconIndex + Environment.NewLine + "The MSI will still be created but the main icon will be missing.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    FilesToDelete.Add(IconFilePath);
                }

                if (_remoteApp.FileTypeAssociations != null)
                {
                    foreach (RemoteAppLib.New.FileTypeAssociation fta in _remoteApp.FileTypeAssociations)
                    {
                        ExtractFtIcon(ProductFileName, fta);
                        FilesToDelete.Add(ProductFileName + "." + fta.Extension + ".ico");
                    }

                    RDP.FlatFileTypes = _remoteApp.FileTypeAssociations.GetFlatFileTypes();
                }

                RDP.CreateMSI(MSIPath);
                RemoteAppFunction.DeleteFiles(FilesToDelete);
                Close();
            }
        }