Click here to Skip to main content
15,920,708 members
Please Sign up or sign in to vote.
4.75/5 (4 votes)
See more:
I have one USB Dongle contain one file data.I want use C# to Get data in this file to Process.

[Edit]Title changed to make it less vague[Edit]
Posted
Updated 7-Apr-10 15:46pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Dec-10 21:06pm    
Extremely confusing title of the question. USB programmng is working with USB controller -- much wanted by difficult stuff. What you want seems to be quite simple.

1 solution

You could start with something like this:

C#
string searchFile = "searchThisFile.txt";
string searchFilePath = "";

bool found;

System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();

foreach (System.IO.DriveInfo drive in drives)
{
    if (drive.DriveType == System.IO.DriveType.Removable)
    {
        if (drive.IsReady)
        {
            string[] files = System.IO.Directory.GetFiles(drive.Name);

            foreach(string file in files)
            {
                if (System.IO.Path.GetFileName(file) == searchFile)
                {
                    searchFilePath = file;
                    found = true;

                    break;
                }
            }
        }

        if(found)
        {
            break;
        }
    }
}

if(found)
{

// File found!
// The path is inside the string
// Process it

}


The code right here gets all removeable drives (USB sticks etc.) and searches for the file you want to process (searchFile). If it finds it it stops searching and store the found path inside searchFilePath.

Found will be set to true, this because otherwise it would still be searching all other drives which you don't want.

Hope this helped,
Good luck

Jordy "Kaiwa" Ruiter
 
Share this answer
 
v2
Comments
Sandeep Mewara 2-Sep-10 13:57pm    
Comment from one OP: hi

Your code does not detect usb dongle

thanks in advance
Sergey Alexandrovich Kryukov 18-Dec-10 21:07pm    
Any idea how to tell USB drive from others? I thought that was something the author of the question wanted...
larruda43 6-May-15 10:46am    
Excelent solution, works perfectly.

Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900