.NET Framework는 드라이브가 SD 카드 (I 같은, 매우 낮은 수준의 progaming 않고, 그래서 전혀하지 적어도 할 수있는 신뢰할 수있는 방법이 의심이다 식별 할 수있는 방법을 제공하지 않습니다 시스템 드라이버 쿼리). 당신이 할 수있는 최선 DriveType.Removable
동일하게 DriveInfo
의 DriveType
속성을 확인하는 것입니다, 그러나 이것은도 등
는하지만 그렇다하더라도, 당신은 적절한 SD 카드를 선택하는 몇 가지 다른 정보가 필요합니다 (
을 생각하는 모든 플래시 드라이브를 선택합니다 컴퓨터에 둘 이상의 SD 카드가 삽입되어있을 수 있음). SD 카드에 볼륨 레이블이 항상 같으면 올바른 드라이브를 선택하는 데 사용할 수 있습니다. 그렇지 않으면 아래 그림과 같이 사용자가 사용하고자하는 이동식 드라이브를 사용자에게 묻습니다.
질문이 지정되지 않은 경우 last file
의 의미는 무엇입니까? 마지막으로 만든 파일, 마지막으로 수정 한 파일, 운영 체제에서 마지막으로 열거 한 파일 또는 파일 이름에서 가장 큰 번호의 파일입니까? 그래서 가장 큰 숫자의 파일을 원한다고 가정합니다.
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);
}
}