1. 程式人生 > 其它 >如何複製word的圖文到xhEditor中自動上傳

如何複製word的圖文到xhEditor中自動上傳

專案工程檔案下載: 工程檔案下載地址

看了很多網上的部落格,講述如何用C#進行USB裝置操作,很多都是不對的。以至於南轅北轍。我們可以使用usb庫。在c下有usblib庫,在C#下該如何使用libusb呢,下面介紹C#下的強大的開源USB類庫就登場了:LibUSBDotNet,沒錯就是.NET下的libusb,這也是個開源專案,已經把libusb封裝成了一個完整的類庫它是sourceforge上的一個開源專案,下載WIN下的EXE安裝即可,裡面包含了很多的範例,還有說明文件(CHM格式的,超級方便的)。下面簡單介紹一下該如何使用LibUSBDotNet。
1、首先你需要建立一個C#的應用程式(控制檯、窗體都可以)

2、將LibUsbDotNet安裝目錄下Src目錄下LibWinUsb拷貝一份到你的工程根目錄下

3、不需要多說了吧,在你的解決方案上右擊,新增現有專案,將LibWinUsb目錄下的專案包含進來

4、在你的專案上右擊,新增引用,選擇LibUSBDotNet專案,如下圖:

下面提供一個讀取資料的範例:筆者插上了一個U盤,用bushound檢查到該U盤的VID和PID:

然而,單純這樣是不夠的,我們還需要用到usblib的工具,註冊這個裝置才能使用。libusb-win32 filter這個工具,只有在這個工具裡面選擇過的裝置,才能是用libusbdotnet的庫函式開啟。

那麼VID是0x0930,PID是0x6544.填入函式中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibUsbDotNet;
using LibUsbDotNet.Main;
using LibUsbDotNet.Info;
using LibUsbDotNet.Descriptors;
using LibUsbDotNet.LibUsb;
using LibUsbDotNet.WinUsb;
namespace USBTest
{
internal class Program
{
public static UsbDevice MyUsbDevice;


#region SET YOUR USB Vendor and Product ID!


public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0x930,0x6544);//U盤ID,可以通過Bushound讀出來


#endregion


public static void Main(string[] args)
{
ErrorCode ec = ErrorCode.None;


try
{
// Find and open the usb device.
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);


// If the device is open and ready
if (MyUsbDevice == null) throw new Exception("Device Not Found.");


// If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
// it exposes an IUsbDevice interface. If not (WinUSB) the
// 'wholeUsbDevice' variable will be null indicating this is
// an interface of a device; it does not require or support
// configuration and interface selection.
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// This is a "whole" USB device. Before it can be used,
// the desired configuration and interface must be selected.


// Select config #1
wholeUsbDevice.SetConfiguration(1);


// Claim interface #0.
wholeUsbDevice.ClaimInterface(0);
}


// open read endpoint 1.
UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);




byte[] readBuffer = new byte[1024];
while (ec == ErrorCode.None)
{
int bytesRead;


// If the device hasn't sent data in the last 5 seconds,
// a timeout error (ec = IoTimedOut) will occur.
ec = reader.Read(readBuffer, 5000, out bytesRead);


if (bytesRead == 0) throw new Exception(string.Format("{0}:No more bytes!", ec));
Console.WriteLine("{0} bytes read", bytesRead);


// Write that output to the console.
Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
}


Console.WriteLine("\r\nDone!\r\n");
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
}
finally
{
if (MyUsbDevice != null)
{
if (MyUsbDevice.IsOpen)
{
// If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
// it exposes an IUsbDevice interface. If not (WinUSB) the
// 'wholeUsbDevice' variable will be null indicating this is
// an interface of a device; it does not require or support
// configuration and interface selection.
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// Release interface #0.
wholeUsbDevice.ReleaseInterface(0);
}


MyUsbDevice.Close();
}
MyUsbDevice = null;


// Free usb resources
UsbDevice.Exit();


}


// Wait for user input..
Console.ReadKey();
}
}
}
}
專案工程檔案下載:工程檔案下載地址
————————————————
版權宣告:本文為CSDN博主「奧利奧冰茶」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/shizhibuyi1234/article/details/77943479

漫思