0

I am working on a project where I need to access some specific addresses of a USB drive (e.g. sector 0), and change some bytes. I have done some parts with C# already that includes user interface, detection of USB drives etc. Can anybody help me providing some links so that I can access specific addresses of USB drives with .NET?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
James
  • 3
  • 2

1 Answers1

0

The Framework doesn't support this. If you attempt to create a FileStream on a device it will throw an exception. You will have to use the Windows API methods directly (p/invoke CreateFile, DeviceIoControl, etc). Make sure you read the section on Physical Disks and Volumes here: http://msdn.microsoft.com/en-us/library/aa363858.aspx

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Tergiver
  • 14,171
  • 3
  • 41
  • 68
  • please don't post old links. Don't post MSDN links that have version numbers in them, unless you're actually _trying_ to provide old information. – John Saunders Jul 23 '10 at 18:29
  • Sorry, I'm not privy to MSDNs URL naming convention. That link looks current from my view. – Tergiver Jul 23 '10 at 19:05
  • Ah, I see it was the (v=VS.85) in the link. I'll try to pay attention in the future. – Tergiver Jul 23 '10 at 19:18
  • Thanks for your answer. But can i call this from my c# code? Can you provide any link that shows C# with win API? – James Jul 24 '10 at 13:18
  • Tutorial on p/invoke: http://msdn.microsoft.com/en-us/magazine/cc164123.aspx. CreateFile @ pinvoke.net: http://www.pinvoke.net/default.aspx/kernel32/CreateFile.html. Pinvoke.net is a 'fair' place to lookup API signatures for C# (and VB). I say 'fair' because there is some incorrect info there (not a lot), and also because with p/invoke, there isn't always just one answer. You can write p/invoke declarations a number of ways, but doing so on your own requires some understanding of how the marshaller works. – Tergiver Jul 24 '10 at 13:38