Search code examples
c#.netwindowsraspberry-piext4

C# write and read files from an EXT4 SD Card


I have a Raspberry Pi SD Card which has 2 Partitions the first one is Fat32 and the second one is EXT4 Partition, now I want to access the files on the EXT4 Partition in C# to write, edit, read and create files. I already tried some things with System.IO to get directories etc.(very basic stuff). So does anyone know if there is a way to access an EXT4 Partition/Disk with C# code?

I am using Windows 10 OS.

It should work like this: File.Create("path to the Disk(J:)/rootfs/home/pi/file.conf");

However, I get an error: System.IO.IOException: "There is no recognized file system on the data carrier


Solution

  • SharpExt4 may help you with Linux file system read and write.

    A .Net library to provide full access (read/write) to Linux ext2/ext3/ext4 filesystem

    Here is the GitHub link https://github.com/nickdu088/SharpExt4

    //Open EXT4 SD Card
    //Here is SD Card physical disk number. you can get from Windows disk manager
    ExtDisk SharpExt4.ExtDisk.Open(int DiskNumber);
    //In your case FAT32 is 1st one, ext4 is 2nd one
    //Open EXT4 partition
    var fs = ExtFileSystem.Open(disk.Parititions[1]); 
    
    //Create /home/pi/file.conf file for write
    var file = fs.OpenFile("/home/pi/file.conf", FileMode.Create, FileAccess.Write);
    var hello = "Hello World";
    var buf = Encoding.ASCII.GetBytes(hello);
    //Write to file
    file.Write(buf, 0, buf.Length);
    file.Close();
    

    How to use SharpExt4 to access Raspberry Pi SD Card Linux partition