本文介绍了使用C#进行USB编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个USB Dongle包含一个文件数据.我想使用C#来获取此文件中的数据以进行处理.

更改标题以使其不那么模糊

I have one USB Dongle contain one file data.I want use C# to Get data in this file to Process.

Title changed to make it less vague

推荐答案

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

}



此处的代码获取所有可移动驱动器(USB记忆棒等)并搜索您要处理的文件(searchFile).如果找到它,它将停止搜索并将找到的路径存储在searchFilePath中.

Found将被设置为true,这是因为否则它仍将搜索您不需要的所有其他驱动器.

希望这对您有所帮助,
祝你好运

Jordy"Kaiwa" Ruiter



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


这篇关于使用C#进行USB编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:11