2017-10-17 7 views
0

私はsdカードファイルにアクセスし、ファイルのすべての内容を読みたいと思います。まず、sdカードの名前を変更できるので、私のコンピュータに接続されたsdカードのパスを書き込めませんでした。私はディレクトリパスのすべてのファイル名を取得したい。SDカードディレクトリ

ディレクトリにファイルがあり、 "1.txt"、 "2.txt"などの名前が付けられています。しかし、私は最後のファイルにアクセスし、最後のファイル行を読み込まなければなりません。私は以下のコードを使用しています。アドバイスはありますか? .NET Frameworkは、このようなとして、私は少なくともない非常に低レベルのprogamingせず、全くそうする信頼性の高い方法がある疑う(SDカードであるドライブ、識別するための方法を提供していません

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

システムドライバの問い合わせ)。あなたができる最善のは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); 
    } 
}