FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


HELP, reading text file from USB drive in visual basic





cvkien
i'm doing a program that when my flash drive plug into the computer, after the computer detect it, then it will read a text file and input it to a text box. how can i do that??

i had try so many ways but it getting my head bigger now.... please help. thanks.
SonLight
cvkien wrote:
i'm doing a program that when my flash drive plug into the computer, after the computer detect it, then it will read a text file and input it to a text box. how can i do that??
.


I'm not quite sure what you're asking for. If you merely want to read a text file from a memory stick, you can read it just like any other file, provided you know the full file name. If you want to detect when the drive is plugged in, that would require doing some OS configuration, and the details would vary, even perhaps between similar OS's like XP and Vista.

Would it be acceptable to ask the user where he wants to read the file from? If so, perhaps you could show a list of devices and let him choose. In the event the memory stick is not plugged in when you ask, the user should be able to plug it in and ask for the list of devices to be refreshed. At worst, you could ask him for the device name. The user can see the assigned device name in the "found new hardware" message, so he can plug it in and then answer with the device name assigned to it.

Perhaps you can get a list of all devices, and look for the file yourself on each one of them, assuming a comparison with name/path/checksum can determine which file you want to use. I know that on older Windows systems, trying that on all devices would lead to a long delay on floppy and cd drives, because they would spin up and look for data even when empty. I think that wouldn't happen with a usb device, but you might have trouble determining which devices should and should not be checked.
Indi
FYI, if you want to detect when a flash/USB/thumb drive is added to your system on Windows (which i assume is the OS you're interested given that you're using VB), i believe all you have to do is request notifications to be sent to a window procedure.

Look up the API function SHChangeNotifyRegister and SHCNE_DRIVEADD. It's really easy to use, but i can't give any help.
cvkien
ok, actually it need to add sysinfo from the tools component, so we can use that to detect any devices that plugin into the usb port, but my problems is when it detect the devices, i don't know how to show which dirve is it. so let say i had 2 usb port, and i had c, d partition drive and e as cdrom, so when i plug in the usb drive, then it should be show f in my computer as the letter for usb drive, but then, how i wanna write the variable drive letter into visual basic and how i can read, for example, txt files inside the usb drive??
cvkien
and then when it detect that the usb drive has a text file, for example text.txt, then i want the program to automatically copy the content inside the text.txt, let say "hello world" into the text box inside my visual basic program, so how can i do that??
Trickster
You can go to the components menu and select the "Microsoft SysInfo Control 6.0" and press ok, and then place that component on your form. Double-click the control on the form and choose the DeviceArrival from the drop-down. All code inside that sub will be fired whenever a new device is added to the computer.
For opening a text.txt file on that USB you could use the Open function, if you know which Drive letter the USB will always have when added, like e.g.: (HSmile

Code:
Dim Text As String

Open H:\Text.txt For Input As #1 'Opens your desired textfile.
    Do While Not EOF(1) 'Runs a loop for each line until end of file is reached.
        Line Input #1, Text 'Each line is set to the string variable "Text".
            TXT_Textbox.Text = TXT_Textbox.Text & Text & Chr(13) & Chr(10) 'Adds each line from the "Text" variable to the textbox, and moves to next line.
    Loop
Close #1


Hope this helps you.
cvkien
erm, you did help a bit but i need 1 more thing, which is the drive letter to a variable. let say normally my pendrive letter is F, but when i plug in my external harddisk, then when i plugin my pen drive, then the letter would change to G. so in this case, it would be better if i detect the pendrive, then the program determine which devices was plug in, and what letter it was.

so now my problem is, how am i going to write a program that can detect any pen drive that plugin into the usb port and then recognize what drive letter they are?

Thanks.
Indi
cvkien wrote:
erm, you did help a bit but i need 1 more thing, which is the drive letter to a variable. let say normally my pendrive letter is F, but when i plug in my external harddisk, then when i plugin my pen drive, then the letter would change to G. so in this case, it would be better if i detect the pendrive, then the program determine which devices was plug in, and what letter it was.

so now my problem is, how am i going to write a program that can detect any pen drive that plugin into the usb port and then recognize what drive letter they are?

Thanks.

Answer ↑
Related topics
Microsoft Visual Basic help
C#,Visual Basic.NET,Visual Studio.NET Videos
MS Visual Basic Studio 2005 Express Edition
Visual Basic Tutorial - File I/O in VB: Text Files
Visual Basic Tutorial - Control Arrays: What, Why and How
Visual Basic Tutorial - Looping
Visual Basic programming tutorial
styling text string read from plain text file
how to use winsock in visual basic????????
how to create a simple chat using winsock in visual basic???
Reading from text file into Flash
VB Help for those who need small pointers in Visual Basic
VB 6 problem executing a .exe program via a visual basic app
Visual Basic RUles
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.