Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            SCardContext ctx = new SCardContext();

            ctx.Establish(SCardScope.System);

            string[] readernames = ctx.GetReaders();
            if (readernames == null || readernames.Length < 1)
            {
                throw new Exception("You need at least one reader in order to run this example.");
            }

            // Receive the ATR of each reader by using the GetAttrib function
            foreach (string name in readernames)
            {
                SCardReader reader = new SCardReader(ctx);

                Console.Write("Trying to connect to reader.. " + name);

                // Connect to the reader, error if no card present.
                SCardError rc = reader.Connect(
                    name,
                    SCardShareMode.Shared,
                    SCardProtocol.Any);

                if (rc == SCardError.Success)
                {
                    // Reader is now connected.
                    Console.WriteLine(" done.");

                    // receive ATR string attribute
                    byte[] atr;
                    rc = reader.GetAttrib(SCardAttr.ATRString, out atr);

                    if (rc != SCardError.Success)
                    {
                        // ATR not supported?
                        Console.WriteLine("Error by trying to receive the ATR. "
                                          + SCardHelper.StringifyError(rc) + "\n");
                    }
                    else
                    {
                        Console.WriteLine("ATR: " + StringAtr(atr) + "\n");
                    }

                    // Disconnect
                    reader.Disconnect(SCardReaderDisposition.Leave);
                }
                else
                {
                    // Probably no SmartCard present.
                    Console.WriteLine(" failed. " + SCardHelper.StringifyError(rc) + "\n");
                }
            }

            ctx.Release();
            return;
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            var context = new SCardContext();

            context.Establish(SCardScope.System);

            var readerNames = context.GetReaders();

            if (readerNames == null || readerNames.Length < 1)
            {
                Console.WriteLine("You need at least one reader in order to run this example.");
                Console.ReadKey();
                return;
            }

            // Receive the ATR of each reader by using the GetAttrib function
            foreach (var readerName in readerNames)
            {
                var reader = new SCardReader(context);

                Console.Write("Trying to connect to reader.. " + readerName);

                // Connect to the reader, error if no card present.
                var rc = reader.Connect(readerName, SCardShareMode.Shared, SCardProtocol.Any);

                if (rc != SCardError.Success)
                {
                    Console.WriteLine(" failed. No smart card present? " + SCardHelper.StringifyError(rc) + "\n");
                }
                else
                {
                    Console.WriteLine(" done.");

                    // receive ATR string attribute
                    byte[] atr;
                    rc = reader.GetAttrib(SCardAttribute.AtrString, out atr);

                    if (rc != SCardError.Success)
                    {
                        // ATR not supported?
                        Console.WriteLine("Error by trying to receive the ATR. {0}\n", SCardHelper.StringifyError(rc));
                    }
                    else
                    {
                        Console.WriteLine("ATR: {0}\n", BitConverter.ToString(atr ?? new byte[] {}));
                    }

                    reader.Disconnect(SCardReaderDisposition.Leave);
                }
            }

            // We MUST release here since we didn't use the 'using(..)' statement
            context.Release();
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            var context = new SCardContext();

            context.Establish(SCardScope.System);

            var readerNames = context.GetReaders();

            if (readerNames == null || readerNames.Length < 1)
            {
                Console.WriteLine("You need at least one reader in order to run this example.");
                Console.ReadKey();
                return;
            }

            foreach (var readerName in readerNames)
            {
                var reader = new SCardReader(context);

                Console.Write("Trying to connect to reader.. " + readerName);

                var rc = reader.Connect(readerName, SCardShareMode.Shared, SCardProtocol.Any);
                if (rc != SCardError.Success)
                {
                    Console.WriteLine(" failed. No smart card present? " + SCardHelper.StringifyError(rc) + "\n");
                }
                else
                {
                    Console.WriteLine(" done.");

                    byte[] attribute = null;
                    rc = reader.GetAttrib(SCardAttribute.VendorInterfaceDeviceTypeVersion, out attribute);
                    if (rc != SCardError.Success)
                    {
                        Console.WriteLine("Error by trying to receive attribute. {0}\n", SCardHelper.StringifyError(rc));
                    }
                    else
                    {
                        Console.WriteLine("Attribute value: {0}\n", BitConverter.ToString(attribute ?? new byte[] { }));
                    }

                    reader.Disconnect(SCardReaderDisposition.Leave);
                }
            }

            context.Release();
            Console.ReadKey();
        }
Ejemplo n.º 4
0
 public string GetReaderPort(string reader)
 {
     using (var context = new SCardContext())
     {
         context.Establish(SCardScope.System);
         var        scReader = new SCardReader(context);
         SCardError error    = scReader.Connect(reader, SCardShareMode.Shared, SCardProtocol.Any);
         byte[]     port;
         error = scReader.GetAttrib(SCardAttribute.ChannelId, out port);
         context.Release();
         StringBuilder builder = new StringBuilder();
         foreach (var b in port)
         {
             builder.Append(Convert.ToString(b, 16));
         }
         return(builder.ToString());
     }
 }