1

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

Daniel Geyfman
  • 246
  • 1
  • 10
F1nn-T
  • 67
  • 8
  • And: https://superuser.com/questions/465393/how-to-mount-read-write-an-ext4-partition-on-windows ; https://superuser.com/questions/37512/how-to-read-ext4-partitions-on-windows – Luuk Feb 28 '21 at 12:39
  • Sorry but the answer didn't help me, the libraries I found didn't work the way I want to. – F1nn-T Feb 28 '21 at 13:39
  • Then please add some code explaining what is not working "the way you want it to do" – Luuk Feb 28 '21 at 13:43
  • Responding to https://superuser.com/questions/465393/how-to-mount-read-write-an-ext4-partition-on-windows and https://superuser.com/questions/37512/how-to-read-ext4-partitions-on-windows. I know there a some programs to do what I want, but I want a solution in C# Code to read edit etc maybe my Question wasn't understandable i edited it for better understanding. – F1nn-T Feb 28 '21 at 13:44
  • If you install something like [sshfs-win](https://github.com/billziss-gh/sshfs-win), you can mount the ext4 partition from windows. Accessing files on it should work in C# (or any other way, because disk is mounted as a network drive) – Luuk Feb 28 '21 at 14:20
  • I tried to share the disk where the partition is then running this in cmd: ```.\sshfs.exe user@myip:139 \\Desktop-test\j``` The Error I get is: Cannot create WinFsp-FUSE file system: invalid mount point. – F1nn-T Feb 28 '21 at 14:52
  • I found a way to mount it with the Linux Reader software. But now I need a way in c# to mount it instead of Linux Reader so everything happens automatic – F1nn-T Feb 28 '21 at 15:01
  • or you could do (from Windows) `scp pi@raspberrypi:/etc/file.conf .` which copies file.conf to your current directory. then edit it, and copy it back. How to do scp is explained here: https://stackoverflow.com/questions/651399/scp-for-c-sharp (and probably on some other places too) – Luuk Feb 28 '21 at 15:11
  • If `scp` is not available on your Windows, then take a look at: https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse – Luuk Feb 28 '21 at 15:13
  • You will have to do some basic research into what a file system is, how it relates to the OS, what runtime environment you are targeting, and how to get a file system call from the CLR into the OS' file system stack. – IInspectable Feb 28 '21 at 15:43

1 Answers1

3

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

Nick
  • 96
  • 5