Reputation: 3
I want to access to sd card files and read all contents of the file. First I could not write the path of sd card plugged to my computer, because sd card names can be changed. Besides I want to get all file names in my directory path.
There are too more files in the directory and they are named with numbers like "1.txt", "2.txt". But I have to access the last file and read the last file lines. I am using the code below. Any advise?
public void readSDcard()
{
//here i want to get names all files in the directory and select the last file
string[] fileContents;
try
{
fileContents = File.ReadAllLines("F:\\MAX\\1.txt");// here i have to write any sd card directory path
foreach (string line in fileContents)
{
Console.WriteLine(line);
}
}
catch (FileNotFoundException ex)
{
throw ex;
}
}
Upvotes: 0
Views: 456
Reputation: 6207
.NET Framework does not provide a way to identify, which drive is a SD Card (I doubt there is a reliable way to do so at all, at least not without very low-level progaming, such as querying system driver). Best you can do is to check DriveType
property of DriveInfo
to be equal to DriveType.Removable
, but this will also select all flash drives etc.
But even then, you will need some another information to select proper SD card (think, there could be more than one SD card inserted in the computer). If SD card has volume label that will be always the same, you can use it to choose the right drive. Otherwise, you will have to ask user, which of the removable drives he wishes to use, as shown bellow.
Question does not specify, what does last file
mean. Is it last created file, last modified file, last file enumerated by operating system, or file with the biggest number in it's file name? So I assume you want a file with the biggest number.
public void readSDcard()
{
var removableDives = System.IO.DriveInfo.GetDrives()
//Take only removable drives into consideration as a SD card candidates
.Where(drive => drive.DriveType == DriveType.Removable)
.Where(drive => drive.IsReady)
//If volume label of SD card is always the same, you can identify
//SD card by uncommenting following line
//.Where(drive => drive.VolumeLabel == "MySdCardVolumeLabel")
.ToList();
if (removableDives.Count == 0)
throw new Exception("No SD card found!");
string sdCardRootDirectory;
if(removableDives.Count == 1)
{
sdCardRootDirectory = removableDives[0].RootDirectory.FullName;
}
else
{
//Let the user select, which drive to use
Console.Write($"Please select SD card drive letter ({String.Join(", ", removableDives.Select(drive => drive.Name[0]))}): ");
var driveLetter = Console.ReadLine().Trim();
sdCardRootDirectory = driveLetter + ":\\";
}
var path = Path.Combine(sdCardRootDirectory, "MAX");
//Here you have all files in that directory
var allFiles = Directory.EnumerateFiles(path);
//Select last file (with the greatest number in the file name)
var lastFile = allFiles
//Sort files in the directory by number in their file name
.OrderByDescending(filename =>
{
//Convert filename to number
var fn = Path.GetFileNameWithoutExtension(filename);
if (Int64.TryParse(fn, out var fileNumber))
return fileNumber;
else
return -1;//Ignore files with non-numerical file name
})
.FirstOrDefault();
if (lastFile == null)
throw new Exception("No file found!");
string[] fileContents = File.ReadAllLines(lastFile);
foreach (string line in fileContents)
{
Console.WriteLine(line);
}
}
Upvotes: 3