0

I am using Visual Studio Team System 2008, C#, .NET 3.5, IIS 7.0, and ASP.NET. I am using Silverlight 3.0 as well. I want to read the content of a USB flash disk at the client side, using the information in the USB flash disk as a user profile identifier -- just like an online bank service is using a USB key to store a client certificate (but my security requirement is not that high).

How the content of a specific file in a USB flash disk in a web application (at the client side) be read? Could we do this in Silverlight (if can not, any alternative solution to read USB flash disk content)?

BTW: I want to read the content automatically, and I do not want the user to manually select the specific file on the USB flash disk to read.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
George2
  • 44,761
  • 110
  • 317
  • 455

2 Answers2

7

You can not do this in Silverlight. You can read a file on the user's machine, but not without user intervention. The security model built into Silverlight will not allow it.

The best you can do is to read a user-specified file is to have them browse to it with a file-open dialog box.

You can access the USB/flash drive if you are using a console/Windows Forms/WPF application locally. You can find the drives using the DriveInfo class, then iterate over them or use a LINQ query to find the drive you want to access. See the Stack Overflow question "How to find USB drive letter?" for details. Once you have a path, you can search it for the file you want. You do not need a special API or library, the Windows OS treats the USB drive as a normal drive, same as a "permanent" HDD connected to your system, and will do all the dirty work for you at that level. Just use the .NET I/O classes. However, if you wish to access the drive in an independent manner that does not depend on any OS, then you should use a 3rd party library.

Community
  • 1
  • 1
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
  • Thanks Chris, if I am developing a client side application (console application), is it possible to enumerate all USB drives and find one which has the specific file inside to read? My confusion is, I do not know how to identify/enumerate all USB flash disks of local computer. – George2 Sep 16 '09 at 05:13
  • 1
    Silverlight prevents you from seeing any files/drives on the user's machine. You can however use OpenFileDialog to get a file stream to a user supplied file. – Graeme Bradbury Sep 16 '09 at 07:47
  • Thanks! If I decide to write client side application, like console or Windows Forms application, is it possible to enumerate all USB flash disks to find specific files (like what we could do for normal IDE hard disk)? I am not sure whether there is any restrictions to enumerate all USB flash disks for specific files for console application or Windows Form/WPF application? – George2 Sep 16 '09 at 07:53
  • 1
    sure, you can do it from a console app, or a WPF app. I don't know the details, but I am sure you can Google for the specifics. – Muad'Dib Sep 16 '09 at 13:11
  • Hi Chris, if I just need to read a file from USB Flash disk, I think I just need to use .Net IO API, do we need to use any special library/API? – George2 Sep 16 '09 at 15:45
  • I have this concern because in my computer, the USB Flash disk displays as a normal disk drive and I can access files in the USB Flash disk in normal Windows file explorer. – George2 Sep 16 '09 at 15:46
  • 1
    you can enumerate the drives on your system to locate the USB drive. check this SO article.... http://stackoverflow.com/questions/123927/how-to-find-usb-drive-letter – Muad'Dib Sep 16 '09 at 16:42
  • Thanks Chris, my confusion is, what is the differences between accessing files on USB Flash disk and on normal IDE disk? Should be the same and using the same .Net IO API? – George2 Sep 16 '09 at 17:29
  • A further question is, if accessing USB Flash disk is like normal HDD file, why we need such special API? http://www.icsharpcode.net/OpenSource/SharpUSBLib/ – George2 Sep 16 '09 at 17:31
  • 1
    that library is to allow you to access a USB device in a platform-independent manner. so, if you use that lib, you can run your program on linux, etc. if you are only using windows, you don't need it. just access as a normal file. – Muad'Dib Sep 16 '09 at 18:12
  • Thanks Chris, for USB key used for online bank application, I am wondering if they are still treated as USB Flash disk (i.e. client confidential certificate could be accessed in the form of normal file as well)? Or they are special USB device which is not USB Flash disk? – George2 Sep 17 '09 at 06:12
  • 1
    I don't know if how (from a development standpoint) those bank USB keys work. I imagine they have some kind of driver or something so that the device can't be tampered with. – Muad'Dib Sep 17 '09 at 13:44
  • Hi Chris, I studied your comments and the open source project. I am still confused, accessing USB Flash disk by using file system I/O is very convenient, why botter using such additional library, any benefits? – George2 Sep 17 '09 at 17:34
  • if i use the library instead of built-in functions, then i can build my same program on a different OS (say linux, or MacOS) bc my code will talk to the library, not the OS-dependent built in library. there can also be performance differences, etc. if you are writing a windows app and do not care about other OS's, then just use the built-in .Net classes/methods. – Muad'Dib Sep 17 '09 at 21:54
1

I don't think what you're asking for is possible. If it IS possible, that's scary stuff... To allow a web site to grab stuff without user intervention? Uh-Uh. No way.\

Edit - Added after reading the comment

Using only the standard framework, no. USB support is not something that comes standard. You'd think there would be some classes in the System.IO namespace for USB ports like there are for COM ports, but no such luck. However, there are some libraries that will handle this. Here are some links to get you started.

http://weblogs.asp.net/israelio/archive/2005/08/15/422637.aspx

http://www.icsharpcode.net/OpenSource/SharpUSBLib/

David
  • 72,686
  • 18
  • 132
  • 173
  • Thanks David, if I am developing a client side application (console application), is it possible to enumerate all USB drives and find one which has the specific file inside to read? My confusion is, I do not know how to identify/enumerate all USB flash disks of local computer. – George2 Sep 16 '09 at 05:15
  • Thanks David! If I just need to read a file from USB Flash disk, I think I just need to use .Net IO API, why I need to use the special library you mentioned? Any functional differences between such API and using .Net IO API? – George2 Sep 16 '09 at 15:44
  • I have this concern because in my computer, the USB Flash disk displays as a normal disk drive and I can access files in the USB Flash disk in normal Windows file explorer. – George2 Sep 16 '09 at 15:48