protected DokanError WritePayload(byte[] payload, string extension)
        {
            // Make sure the chip is still here
            if (!rfidListener.ReconnectOnCard())
            {
                log.Error("Card reconnection failed");
                return(DokanError.ErrorNotReady);
            }

            IChip        chip = rfidListener.GetChip();
            ICardService svc  = chip.GetService(CardServiceType.CST_NFC_TAG);
            IDESFireEV1NFCTag4CardService nfcsvc = svc as IDESFireEV1NFCTag4CardService;

            if (nfcsvc == null)
            {
                log.Error("No NFC service.");
                // If no NFC service for this chip, we must fail
                return(DokanError.ErrorNotReady);
            }

            try
            {
                IStorageCardService storage = chip.GetService(CardServiceType.CST_STORAGE) as IStorageCardService;
                if (storage != null)
                {
                    storage.Erase();
                }
                nfcsvc.CreateNFCApplication(1, null);

                NdefMessage ndef = new NdefMessage();
                switch (extension)
                {
                case "txt":
                    log.Info("Adding Text Record");
                    ndef.AddRawTextRecord(payload);
                    break;

                case "url":
                    string lnk    = Encoding.UTF8.GetString(payload);
                    int    urlpos = lnk.IndexOf("URL=");
                    if (urlpos > -1)
                    {
                        urlpos += 4;
                        int end = lnk.IndexOf(Environment.NewLine, urlpos);
                        if (end < 0)
                        {
                            end = lnk.Length - urlpos;
                        }
                        else
                        {
                            end -= urlpos;
                        }
                        string url = lnk.Substring(urlpos, end).Trim();
                        log.Info(String.Format("Adding Uri Record `{0}`", url));
                        ndef.AddUriRecord(url, UriType.NO_PREFIX);
                    }
                    else
                    {
                        log.Warn("Cannot found URL on link file content.");
                    }
                    break;

                default:
                    string mime = System.Web.MimeMapping.GetMimeMapping("dummy." + extension);
                    log.Info(String.Format("Adding Mime Record `{0}`", mime));
                    ndef.AddMimeMediaRecord(mime, Encoding.ASCII.GetString(payload));       // This shouldn't be a string here
                    break;
                }
                nfcsvc.WriteNDEFFile(ndef);

                ResetCache();
            }
            catch (COMException ex)
            {
                log.Info("NDEF write error", ex);
            }

            return(DokanError.ErrorSuccess);
        }
        protected DokanError ReadPayload(out byte[] payload, out string extension)
        {
            payload   = null;
            extension = String.Empty;

            // Make sure the chip is still here
            if (!rfidListener.ReconnectOnCard())
            {
                log.Error("Card reconnection failed");
                return(DokanError.ErrorNotReady);
            }

            IChip        chip = rfidListener.GetChip();
            ICardService svc  = chip.GetService(CardServiceType.CST_NFC_TAG);
            IDESFireEV1NFCTag4CardService nfcsvc = svc as IDESFireEV1NFCTag4CardService;

            if (nfcsvc == null)
            {
                log.Error("No NFC service.");
                // If no NFC service for this chip, we must fail
                return(DokanError.ErrorNotReady);
            }

            try
            {
                NdefMessage ndef = nfcsvc.ReadNDEFFile();
                if (ndef != null)
                {
                    if (ndef.GetRecordCount() > 0)
                    {
                        // For now, only support for one Ndef record per card
                        object[] records = ndef.Records as object[];
                        if (records != null)
                        {
                            INdefRecord record     = records[0] as INdefRecord;
                            object      payloadobj = record.Payload;
                            if (payloadobj != null)
                            {
                                payload = payloadobj as byte[];
                                payload = ParsePayload(record, payload, out extension);
                            }
                            else
                            {
                                log.Error("Bad record payload");
                            }
                        }
                    }
                    else
                    {
                        log.Info("No NDEF record in the message");
                    }
                }
                else
                {
                    log.Info("No NDEF message on this card");
                }
            }
            catch (COMException ex)
            {
                log.Info("NDEF read error", ex);
            }

            return(DokanError.ErrorSuccess);
        }