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?
A:
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
Tergiver
2010-07-23 18:26:29
@Tergiver: 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
2010-07-23 18:29:48
Sorry, I'm not privy to MSDNs URL naming convention. That link looks current from my view.
Tergiver
2010-07-23 19:05:35
Ah, I see it was the (v=VS.85) in the link. I'll try to pay attention in the future.
Tergiver
2010-07-23 19:18:15
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
2010-07-24 13:18:14
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
2010-07-24 13:38:56