资源描述
USB在日常中已经广泛被使用,手机,MP3,MP4,相机等都采用了USB接口。本文向大家介绍一个C#实现的USB接口操作类。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
USB usb;
usb = new UDisk();//插入U盘
usb.OutputFile();//从U盘读出文件
usb.InputFile();//往U盘写入文件
usb.Dispose();//拔出U盘
Console.WriteLine("");
usb = new MDisk();//插入移动硬盘
usb.OutputFile();//从移动硬盘读出文件
usb.InputFile();//往移动硬盘写入文件
usb.Dispose();//拔出移动硬盘
Console.WriteLine("");
usb = new MP4();//插入MP4
usb.OutputFile();//从MP4读出文件
usb.InputFile();//往MP4写入文件
usb.Dispose();//拔出MP4
Console.ReadKey();
}
//USB接口
public interface USB : IDisposable
{
void OutputFile();//读出文件
void InputFile();//写入文件
}
//U盘
public class UDisk : USB
{
public UDisk()
{
Console.WriteLine("U盘准备就绪...");
}
public void OutputFile()
{
Console.WriteLine("从U盘读出文件");
}
public void InputFile()
{
Console.WriteLine("往U盘写入文件");
}
public void Dispose()
{
Console.WriteLine("U盘已被拔出");
}
}
//移动硬盘
public class MDisk : USB
{
public MDisk()
{
Console.WriteLine("移动硬盘准备就绪...");
}
public void OutputFile()
{
Console.WriteLine("从移动硬盘读出文件");
}
public void InputFile()
{
Console.WriteLine("往移动硬盘写入文件");
}
public void Dispose()
{
Console.WriteLine("移动硬盘已被拔出");
}
}
//MP4
public class MP4 : USB
{
public MP4()
{
Console.WriteLine("MP4准备就绪...");
}
public void OutputFile()
{
Console.WriteLine("从MP4读出文件");
}
public void InputFile()
{
Console.WriteLine("往MP4写入文件");
}
public void Dispose()
{
Console.WriteLine("MP4已被拔出");
}
}
}
}
用C#写的一个读写USB口操作的类
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
classClass1
{
[DllImport("kernel32.dll")]
privatestaticexternIntPtr CreateFile(
String lpFileName,
UInt32 dwDesiredAccess,
UInt32 dwShareMode,
IntPtr lpSecurityAttributes,
UInt32 dwCreationDisposition,
UInt32 dwFlagsAndAttributes,
IntPtr hTemplateFile
);
[DllImport("Kernel32.dll")]
privatestaticexternbool ReadFile(
IntPtr hFile,
byte[] lpBuffer,
uint nNumberOfBytesToRead,
refuint lpNumberOfBytesRead,
IntPtr lpOverlapped
);
[DllImport("Kernel32.dll")]
privatestaticexternbool WriteFile(
IntPtr hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
refuint lpNumberOfBytesWritten,
IntPtr lpOverlapped
);
[DllImport("kernel32.dll")]
privatestaticexternbool CloseHandle(
IntPtr hObject
);
//--------------------------------------------------------------------------------
IntPtr hFile;
privateconstUInt32 GENERIC_READ = 0x80000000;
privateconstUInt32 GENERIC_WRITE = 0x40000000;
privateconstUInt32 OPEN_EXISTING = 3;
privateconstInt32 INVALID_HANDLE_VALUE = -1;
privateconstint USB_WRITENUM = 8;
privateconstint USB_READNUM = 8;
privatebyte[] m_rd_data = newbyte[USB_READNUM];
publicbyte[] rd_data
{
get { return m_rd_data; }
set { m_rd_data = value; }
}
privatebyte[] m_wr_data = newbyte[USB_WRITENUM];
publicbyte[] wr_data
{
get { return m_wr_data; }
set { m_wr_data = value; }
}
publicbool 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;
}
publicbool USBDataRead()
{
uint read = 0;
return ReadFile(hFile, m_rd_data, (uint)USB_READNUM, ref read, IntPtr.Zero);
}
publicbool USBDataWrite()
{
uint written = 0;
return WriteFile(hFile, m_wr_data, (uint)USB_WRITENUM, ref written, IntPtr.Zero);
}
publicvoid CloseConnection()
{
if (hFile.ToInt32() != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
hFile = IntPtr.Zero;
}
}
}
}
5 / 5
展开阅读全文