C# .NET - how to acess USB port in C#.net - Asked By aman on 23-Aug-11 02:04 AM

hi all,
i want to deal with USB in my windows application but not getting right idea .......how to access and make use of it

please help me with some good example or if possible with some code

thanks and regards
Aman
Ravi S replied to aman on 23-Aug-11 02:06 AM
Hi

Try this sample to communicate with serail port-

Add following namespaces-

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
Now use this code-

void Button1Click(object sender, EventArgs e)
{
//configuring the serial port
serialPort1.PortName="COM1";
serialPort1.BaudRate=9600;
serialPort1.DataBits=8;
serialPort1.Parity=Parity.None;
serialPort1.StopBits= StopBits.One;

//opening the serial port
serialPort1.Open();

//write data to serial port
serialPort1.Write("ABC");

//Read data to serial port


string str= serialPort1.Read();

//close the port
serialPort1.Close();

}

hope this will help you.


refer
http://www.eggheadcafe.com/community/aspnet/95/10340733/how-to-read-usb-port-in-net.aspx
Reena Jain replied to aman on 23-Aug-11 02:07 AM
Hi,

Here is a simple code for you to access USB port

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
Now use this code-
 
void Button1Click(object sender, EventArgs e)
{
//configuring the serial port
serialPort1.PortName="COM1";
serialPort1.BaudRate=9600;
serialPort1.DataBits=8;
serialPort1.Parity=Parity.None;
serialPort1.StopBits= StopBits.One;
 
//opening the serial port
serialPort1.Open();
 
//write data to serial port
serialPort1.Write("ABC");
 
//Read data to serial port
 
string str= serialPort1.Read();
 
//close the port
serialPort1.Close();
 
}

Hope this will help you
Web Star replied to aman on 23-Aug-11 02:08 AM
You can detect USB throuth windows registy. 

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USBSTOR

For detect you can create a Listener.
Also see these link for more details
Look this articles:

http://blogs.msdn.com/coding4fun/archive/2006/10/31/912422.aspx 
http://www.developerfusion.co.uk/show/4392/5/ 
http://www.icsharpcode.net/OpenSource/SharpUSBLib/default.aspx 
http://weblogs.asp.net/israelio/archive/2005/08/15/422637.aspx
Ravi S replied to aman on 23-Aug-11 02:08 AM
HI

Here are the steps needed to add this functionality into your program without worrying about how it works:
 
  * Add the source code (DriveDetector.cs) to your project and add using Dolinay; directive to your Form's source code.
  * Create an instance of the DriveDetector class in your form. See example below.
  * Add handlers for the events exposed by DriveDetector, that is for DeviceArrived, DeviceRemoved and QueryRemove.
    Collapse | Copy Code
 
    public partial class Form1 : Form
    {
      private DriveDetector driveDetector = null;
 
      public Form1()
      {
        InitializeComponent();
        driveDetector = new DriveDetector();
        driveDetector.DeviceArrived += new DriveDetectorEventHandler(
          OnDriveArrived);
        driveDetector.DeviceRemoved += new DriveDetectorEventHandler(
          OnDriveRemoved);
        driveDetector.QueryRemove += new DriveDetectorEventHandler(
          OnQueryRemove);
              ...
 
  * Implement these handlers as shown here. You can simply copy and paste this block of source code to your Form class.
    Collapse | Copy Code
 
    // Called by DriveDetector when removable device in inserted
 
    private void OnDriveArrived(object sender, DriveDetectorEventArgs e)
    {
      // e.Drive is the drive letter, e.g. "E:\\"
 
      // If you want to be notified when drive is being removed (and be
 
      // able to cancel it),
 
      // set HookQueryRemove to true
 
      e.HookQueryRemove = true;
    }
 
    // Called by DriveDetector after removable device has been unplugged
 
    private void OnDriveRemoved(object sender, DriveDetectorEventArgs e)
    {
      // TODO: do clean up here, etc. Letter of the removed drive is in
 
      // e.Drive;
 
    }
 
    // Called by DriveDetector when removable drive is about to be removed
 
    private void OnQueryRemove(object sender, DriveDetectorEventArgs e)
    {
      // Should we allow the drive to be unplugged?
 
      if (MessageBox.Show("Allow remove?", "Query remove",
        MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
          DialogResult.Yes)
        e.Cancel = false;      // Allow removal
 
      else
        e.Cancel = true;     // Cancel the removal of the device
 
    }
 
That's all. Your event handlers should now be called whenever a flash drive is inserted or removed.
 
In this default implementation DriveDetector will create a hidden form, which it will use to receive notification messages from Windows. You could also pass your form to DriveDetector's constructor to avoid this extra (although invisible) form. To do this, just create your DriveDetector object using the second constructor:

 
 driveDetector = new DriveDetector(this);
 
But then you also have to override WndPRoc method in your Form's code and call DriveDetector's WndProc from there. Again, you can just copy-paste the code below to your Form1.cs.

 
protected override void WndProc(ref Message m)
{        
  base.WndProc(ref m);      // call default p
 
 
  if (driveDetector != null)
  {
    driveDetector.WndProc(ref m);        
  }
}


refer
http://www.codeproject.com/KB/system/DriveDetector.aspx
Anoop S replied to aman on 23-Aug-11 02:11 AM
If it is a standard USB device it should appear as a drive, so you can save the data to a file just as you would the regular file system.
Whenever you plug USB Device, OS will assign a alphabet character to this drive(suppose G), You need to replace the Drive letter mentioned in red on yellow highlighted line with the drive letter of USB Drive.
Anoop S replied to aman on 23-Aug-11 02:12 AM
sorry forgot to add code

//Use StreamWriter class.
StreamWriter sw = new StreamWriter("E:\test.txt");