コード例 #1
0
        private void RefreshReaderUnits()
        {
            cbReaderUnit.Items.Clear();

            if (cbReaderProvider.SelectedIndex > -1)
            {
                string         provider       = cbReaderProvider.SelectedItem.ToString();
                LibraryManager libmgt         = LibraryManager.getInstance();
                ReaderProvider readerProvider = libmgt.getReaderProvider(provider);
                if (readerProvider != null)
                {
                    ReaderUnitCollection readers = readerProvider.getReaderList();
                    foreach (ReaderUnit reader in readers)
                    {
                        cbReaderUnit.Items.Add(reader.getName());
                    }
                }
            }
        }
コード例 #2
0
ファイル: RFIDKeyProvider.cs プロジェクト: zub3ra/keepassrfid
        public static void ChipAction(Action <Chip> action, KeePassRFIDConfig rfidConfig)
        {
            if (rfidConfig == null)
            {
                rfidConfig = KeePassRFIDConfig.GetFromCurrentSession();
            }

            ReaderProvider readerProvider;

            try
            {
                LibraryManager libmgt = LibraryManager.getInstance();

                readerProvider = libmgt.getReaderProvider(!String.IsNullOrEmpty(rfidConfig.ReaderProvider) ? rfidConfig.ReaderProvider : "PCSC");
                if (readerProvider == null)
                {
                    throw new KeePassRFIDException(Properties.Resources.RFIDConfigurationError);
                }
            }
            catch (COMException ex)
            {
                throw new KeePassRFIDException(Properties.Resources.RFIDInitError, ex);
            }

            ReaderUnit readerUnit = null;

            if (String.IsNullOrEmpty(rfidConfig.ReaderUnit))
            {
                readerUnit = readerProvider.createReaderUnit();
                if (readerUnit == null)
                {
                    throw new KeePassRFIDException(Properties.Resources.RFIDReaderInitError);
                }
            }
            else
            {
                ReaderUnitCollection readers = readerProvider.getReaderList();
                foreach (ReaderUnit reader in readers)
                {
                    if (reader.getName() == rfidConfig.ReaderUnit)
                    {
                        readerUnit = reader;
                        break;
                    }
                }
            }

            if (readerUnit == null)
            {
                throw new KeePassRFIDException(Properties.Resources.RFIDReaderNotFound);
            }

            try
            {
                if (readerUnit.connectToReader())
                {
                    // Don't wait for chip insertion, it must already be on the reader
                    if (readerUnit.waitInsertion(1))
                    {
                        if (readerUnit.connect())
                        {
                            Chip chip = readerUnit.getSingleChip();
                            if (chip != null)
                            {
                                action(chip);
                            }
                            else
                            {
                                ShowError(Properties.Resources.NoChipError);
                            }

                            readerUnit.disconnect();
                        }
                        else
                        {
                            ShowError(Properties.Resources.CardConnectionFailed);
                        }
                        readerUnit.waitRemoval(1);
                    }
                    else
                    {
                        ShowError(Properties.Resources.NoCardInserted);
                    }

                    readerUnit.disconnectFromReader();
                }
                else
                {
                    ShowError(Properties.Resources.ReaderConnectionFailed);
                }
            }
            catch (COMException ex)
            {
                ShowError(ex.Message);
            }

            GC.KeepAlive(readerUnit);
            GC.KeepAlive(readerProvider);
        }