



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
USB在日常中已经广泛被使用,手机,MP3,MP4,相机等都采用了USB接口。本文向大家介绍一个C#实现的USB接口操作类。namespaceConsoleApplication1classProgramstaticvoidMain(stringargs)USBusb;usb=newUDisk();/插入U盘usb.OutputFile();/从U盘读出文件usb.InputFile();/往U盘写入文件usb.Dispose();/拔出U盘Console.WriteLine();usb=newMDisk();/插入移动硬盘usb.OutputFile();/从移动硬盘读出文件usb.InputFile();/往移动硬盘写入文件usb.Dispose();/拔出移动硬盘Console.WriteLine();usb=newMP4();/插入MP4usb.OutputFile();/从MP4读出文件usb.InputFile();/往MP4写入文件usb.Dispose();/拔出MP4Console.ReadKey();/USB接口publicinterfaceUSB:IDisposablevoidOutputFile();/读出文件voidInputFile();/写入文件/U盘publicclassUDisk:USBpublicUDisk()Console.WriteLine(U盘准备就绪.);publicvoidOutputFile()Console.WriteLine(从U盘读出文件);publicvoidInputFile()Console.WriteLine(往U盘写入文件);publicvoidDispose()Console.WriteLine(U盘已被拔出);/移动硬盘publicclassMDisk:USBpublicMDisk()Console.WriteLine(移动硬盘准备就绪.);publicvoidOutputFile()Console.WriteLine(从移动硬盘读出文件);publicvoidInputFile()Console.WriteLine(往移动硬盘写入文件);publicvoidDispose()Console.WriteLine(移动硬盘已被拔出);/MP4publicclassMP4:USBpublicMP4()Console.WriteLine(MP4准备就绪.);publicvoidOutputFile()Console.WriteLine(从MP4读出文件);publicvoidInputFile()Console.WriteLine(往MP4写入文件);publicvoidDispose()Console.WriteLine(MP4已被拔出);用C#写的一个读写USB口操作的类using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace WindowsApplication1 class Class1 DllImport(kernel32.dll) private static extern IntPtr CreateFile( String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode, IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile ); DllImport(Kernel32.dll) private static extern bool ReadFile( IntPtr hFile, byte lpBuffer, uint nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, IntPtr lpOverlapped ); DllImport(Kernel32.dll) private static extern bool WriteFile( IntPtr hFile, byte lpBuffer, uint nNumberOfBytesToWrite, ref uint lpNumberOfBytesWritten, IntPtr lpOverlapped ); DllImport(kernel32.dll) private static extern bool CloseHandle( IntPtr hObject ); /- IntPtr hFile; private const UInt32 GENERIC_READ = 0x80000000; private const UInt32 GENERIC_WRITE = 0x40000000; private const UInt32 OPEN_EXISTING = 3; private const Int32 INVALID_HANDLE_VALUE = -1; private const int USB_WRITENUM = 8; private const int USB_READNUM = 8; private byte m_rd_data = new byteUSB_READNUM; public byte rd_data get return m_rd_data; set m_rd_data = value; private byte m_wr_data = new byteUSB_WRITENUM; public byte wr_data get return m_wr_data; set m_wr_data = value; public bool OnInitUSB() hFile = IntPtr.Zero; string deviceName = string.Empty; deviceName = 在这里写上你的设备的地址; hFile = CreateFile( deviceName, GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero ); return hFile.ToInt32() = INVALID_HANDLE_VALUE ? false : true; public bool USBDataRead() uint read = 0; return ReadFile(hFile, m_rd_data, (uint)USB_READNUM, ref read, IntPtr.Zero); public bool USBDataWrite() uint written = 0; return WriteFile(hFile, m_wr_data, (uint)U
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 责任课件内容大纲
- 2025版上海房地产买卖合同范本涵盖交易税费及支付方式说明
- 2025年房地产营销代理及品牌策划服务合同
- 2025第一部分:生物制药研发合同协议书
- 2025年智能家居产品广告创意合同示范文本
- 2025版跨境贸易借款合同带保证人及汇率风险对冲下载
- 2025年度建筑材料代购与绿色建筑认证服务合同
- 2025版外籍技术人员引进与管理合同书
- 2025年度专业摄影棚租赁服务合同
- 2025版文化娱乐融资咨询与专业居间服务协议
- 2025年有害生物防治员初级理论知识考核试题及答案
- 新版2026统编版小学道德与法治三年级上册 第4课《 科技力量大》第1课时 科技改变生活和科技改变观念 教案设计(教案)
- 学会交流与沟通课件
- 铁路监理培训考试试题及答案
- 2025全国企业员工全面质量管理知识竞赛题库附答案
- 供应链与贸易安全培训课件
- 严禁燃放烟花炮竹课件
- 宫颈息肉课件
- 人工智能多智能体课件
- 人事任命书红头文件模板
- 探讨恶性肿瘤患者化疗后口腔溃疡治疗及护理的有效措施
评论
0/150
提交评论