2017-10-17 1 views
0

sd 카드 파일에 액세스하고 파일의 모든 내용을 읽고 싶습니다. 처음에는 sd 카드 이름을 바꿀 수 있기 때문에 sd 카드의 경로를 내 컴퓨터에 쓸 수 없었습니다. 게다가 나는 디렉토리 경로에있는 모든 파일 이름을 얻고 싶다.SD 카드 디렉토리

디렉토리에 파일이 너무 많으며 이름이 "1.txt", "2.txt"와 같이 지정됩니다. 하지만 마지막 파일에 액세스하고 마지막 파일 행을 읽어야합니다. 아래 코드를 사용하고 있습니다. 어떤 조언?

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; 
      } 
     } 

답변

3

.NET Framework는 드라이브가 SD 카드 (I 같은, 매우 낮은 수준의 progaming 않고, 그래서 전혀하지 적어도 할 수있는 신뢰할 수있는 방법이 의심이다 식별 할 수있는 방법을 제공하지 않습니다 시스템 드라이버 쿼리). 당신이 할 수있는 최선 DriveType.Removable 동일하게 DriveInfoDriveType 속성을 확인하는 것입니다, 그러나 이것은도 등

는하지만 그렇다하더라도, 당신은 적절한 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); 
    } 
}