/// <summary>
        /// Initializes a new instance of the <see cref="SmartCardReaderChooser"/> class.
        /// </summary>
        public SmartCardReaderChooser()
        {
            InitializeComponent();

            Application.ApplicationExit += (s, _) =>
            {
                Action<Action> t = a =>
                {
                    try { a(); }
                    catch { }
                };

                if (reader != null)
                {
                    t(() => reader.CloseCard());
                    t(() => reader.Dispose());
                    reader = null;
                }
            };

            readersBox.SelectedValueChanged += (s, _) => RefreshUI();
            refreshReadersButton.Click += (s, _) => FillReadersList();
            openCardButton.Click += (s, _) => OpenCard();
            closeCardButton.Click += (s, _) => CloseCard();
        }
        private void OpenCard()
        {
            var name = readersBox.SelectedItem as string;
            if (string.IsNullOrEmpty(name))
                return;

            if (reader != null)
            {
                reader.Dispose();
                reader = null;
            }

            try
            {
                reader = PcscSmartCardReaderFactory.CreateDevice(name);
                reader.OpenCard();
                Program.Log(string.Format("Card Opened: {0}", reader.IsCardOpened));
                OnSmartCardReaderConnected();
            }
            catch (Exception ex)
            {
                Program.LogException(ex);
                if (reader != null)
                    reader.Dispose();
                reader = null;
            }

            RefreshUI();
        }
 public byte[] Execute(PcscSmartCardReader reader)
 {
     try
     {
         if (Le.HasValue)
         {
             if (Data != null && Data.Length > 0)
                 return reader.SendCommand(Cla, Ins, P1, P2, Data, Le.Value);
             else return reader.SendCommand(Cla, Ins, P1, P2, Le.Value);
         }
         else
         {
             if (Data != null && Data.Length > 0)
                 return reader.SendCommand(Cla, Ins, P1, P2, Data);
             else return reader.SendCommand(Cla, Ins, P1, P2);
         }
     }
     catch (Exception ex)
     {
         Program.LogException(ex);
         return null;
     }
 }
        private void CloseCard()
        {
            try
            {
                reader.CloseCard();
                Program.Log(string.Format("Card Opened: {0}", reader.IsCardOpened));
            }
            catch (Exception ex)
            {
                Program.LogException(ex);
            }
            finally
            {
                if (reader != null)
                    reader.Dispose();
                reader = null;
                OnSmartCardReaderDisconnected();
            }

            RefreshUI();
        }