Sunday, 14 November 2010

How to Read File Name from Pen/Removable derives using C#

How to Read Data from Pen/Removable derives using C#

In this post I would love to explain about how to read data or files from Removal derives/Pen drives etc.
Let me explain the scenario where I need this code to execute my automation.
Actually there was a scenario where I need to take the screen shot of the printer panel or web page and I have to compare that screen shot with predefined images stored in removal derives or pen drives.

Then let me share the code snippet which I have used to execute my purpose.
Public string getFilelocation(string imagename)
{
try
{
//go through all the driver
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
//check whether the driver is readonly or not
if (drive.IsReady)
{
//verify that the deriver is removable only
if (drive.DriveType.ToString() == "Removable")
{
//read the contents of the removable drive
//check whether folder exit or not
if (Directory.Exists(drive.Name + "DCIM\\100HPAIO"))
{
//get the path of the folder
driverPath = drive.Name + "DCIM\\100HPAIO";
//get all the files available in that folder
string[] fileNames = Directory.GetFiles(driverPath);
if (fileNames.Length > 0)
{
foreach (string filepath in fileNames)
{
String imageName= Path.GetFileName(filepath);
If(imageName==ImageNameTaken)
{
retrun filepath;
}
}

} //end of checking file path in USB

}

}
}
return “FileNot Found”;

}
catch (Exception ex)
{

return ex.Message;
}
}
}

Hope that would be useful.
Thanks,
Md. Jawed

No comments: