基本信息
源码名称:C#读写NFC
源码大小:0.24M
文件格式:.zip
开发语言:C#
更新时间:2025-10-09
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
C#读写NFC

using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using NfcManager;

namespace NfcManagerWinForm
{
    public partial class Form1 : Form
    {
        private PcScNfcManager _nfcManager;
        private bool _isInitialized = false;

        public Form1()
        {
            InitializeComponent();
        }

        private async void Form1_Load(object sender, EventArgs e)
        {
            _nfcManager = new PcScNfcManager();
            await InitializeNfcManagerAsync();
        }

        private async Task InitializeNfcManagerAsync()
        {
            try
            {
                statusLabel.Text = "正在初始化NFC读卡器...";
                statusLabel.ForeColor = System.Drawing.Color.Orange;
                
                await _nfcManager.InitializeAsync();
                _isInitialized = true;
                statusLabel.Text = $"NFC读卡器已就绪: {_nfcManager.ReaderName}";
                statusLabel.ForeColor = System.Drawing.Color.Green;
                readButton.Enabled = true;
                writeButton.Enabled = true;
                clearButton.Enabled = true;
            }
            catch (Exception ex)
            {
                statusLabel.Text = $"NFC读卡器初始化失败";
                statusLabel.ForeColor = System.Drawing.Color.Red;
                readButton.Enabled = false;
                writeButton.Enabled = false;
                clearButton.Enabled = false;
                
                // 显示详细的故障排除建议
                string errorMessage = $"NFC读卡器初始化失败:\n{ex.Message}";
                if (ex.InnerException != null)
                {
                    errorMessage  = $"\n内部错误: {ex.InnerException.Message}";
                }
                
                string troubleshooting = "\n\n故障排除建议:\n"  
                    "1. 检查NFC读卡器是否正确连接到计算机\n"  
                    "2. 确保NFC读卡器驱动程序已正确安装\n"  
                    "3. 启动Windows Smart Card服务:\n"  
                    "   - 按 Win   R,输入 services.msc\n"  
                    "   - 找到并启动 'Smart Card' 服务\n"  
                    "4. 尝试以管理员权限运行此程序\n"  
                    "5. 检查设备管理器中是否有未识别的设备\n"  
                    "6. 重启计算机后再次尝试";
                
                MessageBox.Show(errorMessage   troubleshooting, 
                    "初始化失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
                // 在结果框中也显示错误信息
                resultTextBox.Text = errorMessage   troubleshooting;
            }
        }

        private async void readButton_Click(object sender, EventArgs e)
        {
            if (!_isInitialized) return;

            try
            {
                readButton.Enabled = false;
                statusLabel.Text = "正在读取NFC标签,请将标签靠近读卡器...";
                statusLabel.ForeColor = System.Drawing.Color.Orange;
                resultTextBox.Text = "";

                string content = await _nfcManager.ReadNfcTagAsync();
                resultTextBox.Text = content;
                
                statusLabel.Text = "读取完成";
                statusLabel.ForeColor = System.Drawing.Color.Green;
            }
            catch (Exception ex)
            {
                statusLabel.Text = $"读取失败: {ex.Message}";
                statusLabel.ForeColor = System.Drawing.Color.Red;
                MessageBox.Show($"读取NFC标签时出错:\n{ex.Message}", "读取失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                readButton.Enabled = true;
            }
        }

        private async void writeButton_Click(object sender, EventArgs e)
        {
            if (!_isInitialized) return;

            string url = urlTextBox.Text;
            if (string.IsNullOrWhiteSpace(url))
            {
                MessageBox.Show("请输入要写入的网址", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                writeButton.Enabled = false;
                statusLabel.Text = "正在写入NFC标签,请将标签靠近读卡器...";
                statusLabel.ForeColor = System.Drawing.Color.Orange;
                resultTextBox.Text = "";

                bool success = await _nfcManager.WriteUrlToNfcTagAsync(url);
                if (success)
                {
                    resultTextBox.Text = $"网址 '{url}' 已成功写入NFC标签";
                    statusLabel.Text = "写入完成";
                    statusLabel.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    resultTextBox.Text = "写入失败";
                    statusLabel.Text = "写入失败";
                    statusLabel.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception ex)
            {
                statusLabel.Text = $"写入失败: {ex.Message}";
                statusLabel.ForeColor = System.Drawing.Color.Red;
                MessageBox.Show($"写入NFC标签时出错:\n{ex.Message}", "写入失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                writeButton.Enabled = true;
            }
        }

        private async void clearButton_Click(object sender, EventArgs e)
        {
            if (!_isInitialized) return;

            try
            {
                clearButton.Enabled = false;
                statusLabel.Text = "正在清除NFC标签,请将标签靠近读卡器...";
                statusLabel.ForeColor = System.Drawing.Color.Orange;
                resultTextBox.Text = "";

                bool success = await _nfcManager.ClearNfcTagAsync();
                if (success)
                {
                    resultTextBox.Text = "NFC标签内容已清除";
                    statusLabel.Text = "清除完成";
                    statusLabel.ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    resultTextBox.Text = "清除失败";
                    statusLabel.Text = "清除失败";
                    statusLabel.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception ex)
            {
                statusLabel.Text = $"清除失败: {ex.Message}";
                statusLabel.ForeColor = System.Drawing.Color.Red;
                MessageBox.Show($"清除NFC标签时出错:\n{ex.Message}", "清除失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                clearButton.Enabled = true;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _nfcManager?.Dispose();
        }
        
        /// <summary>
        /// 重新初始化NFC读卡器
        /// </summary>
        private async void reinitializeButton_Click(object sender, EventArgs e)
        {
            // 释放现有资源
            _nfcManager?.Dispose();
            
            // 创建新的NFC管理器实例
            _nfcManager = new PcScNfcManager();
            _isInitialized = false;
            
            // 重新初始化
            await InitializeNfcManagerAsync();
        }
    }
}