C# SD卡目录

C# SD卡目录,c#,C#,我想访问sd卡文件并读取文件的所有内容。首先,我无法写入插入计算机的sd卡路径,因为sd卡名称可以更改。此外,我想得到我的目录路径中的所有文件名 目录中的文件太多了,它们用“1.txt”、“2.txt”等数字命名。但我必须访问最后一个文件并读取最后一行文件。我正在使用下面的代码。有什么建议吗 public void readSDcard() { //here i want to get names all files in the dire

我想访问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;
            }
        }

.NET Framework没有提供一种方法来识别哪个驱动器是SD卡(我怀疑是否有可靠的方法来这样做,至少在没有非常低级的编程(例如查询系统驱动程序)的情况下是如此)。您最好检查
DriveInfo
DriveType
属性是否等于
DriveType.Removable
,但这也会选择所有闪存驱动器等

但即便如此,您也需要一些其他信息来选择合适的SD卡(想想,计算机中可能插入了多个SD卡)。如果SD卡的卷标始终相同,则可以使用它选择正确的驱动器。否则,您必须询问用户,他希望使用哪种可移动驱动器,如下所示

问题没有具体说明,
最后一个文件
是什么意思。它是上次创建的文件、上次修改的文件、操作系统枚举的最后一个文件,还是文件名中数字最大的文件?所以我假设你想要一个数字最大的文件

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

.NET Framework没有提供一种方法来识别哪个驱动器是SD卡(我怀疑是否有可靠的方法来这样做,至少在没有非常低级的编程(例如查询系统驱动程序)的情况下是如此)。您最好检查
DriveInfo
DriveType
属性是否等于
DriveType.Removable
,但这也会选择所有闪存驱动器等

但即便如此,您也需要一些其他信息来选择合适的SD卡(想想,计算机中可能插入了多个SD卡)。如果SD卡的卷标始终相同,则可以使用它选择正确的驱动器。否则,您必须询问用户,他希望使用哪种可移动驱动器,如下所示

问题没有具体说明,
最后一个文件
是什么意思。它是上次创建的文件、上次修改的文件、操作系统枚举的最后一个文件,还是文件名中数字最大的文件?所以我假设你想要一个数字最大的文件

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