views:

350

answers:

2

Hello everyone,

I am using VSTS 2008 + C# + .Net 3.5 + IIS 7.0 + ASP.Net. I am using Silverlight 3.0 as well. I want to read the content of USB Flash disk at client side, the using the information in the USB Flash disk as user profile identifier -- just like online bank service is using USB key to store client certificate (but my security requirement is not that high).

My queston is how to read the content of a specific file in USB Flash disk in web application (at client side)? 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 donot want user to select the specific file in USB Flash disk to read manually.

thanks in advance, George

+7  A: 

you can not do this in Silverlight. you can read a file on the user's machine, but not w/o 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/winforms/WPF app 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 this SO question 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 IO 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.

Muad'Dib
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
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
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
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
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
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
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
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
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
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
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
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
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
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
+1  A: 

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 Stratton
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
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
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