问题描述
我有一个树莓派 SD 卡,它有 2 个分区,第一个是 fat32,第二个是 EXT4 分区,现在我想用 C# 访问 EXT4 分区上的文件来写入、编辑、读取和创建文件。 我已经用 System.IO 尝试了一些东西来获取目录等(非常基本的东西)。 那么有谁知道是否有办法使用 C# 代码访问 EXT4 分区/磁盘?
我使用的是 Windows 10 操作系统。
它应该像这样工作:File.Create("path to the disk(J:)/rootfs/home/pi/file.conf");
但是,我收到一个错误:System.IO.IOException: "There is no recognized file system on the data carrier
解决方法
SharpExt4 可以帮助您进行 Linux 文件系统的读写。
提供对 Linux ext2/ext3/ext4 文件系统的完全访问(读/写)的 .Net 库
这里是GitHub链接 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,buf.Length);
file.Close();
How to use SharpExt4 to access Raspberry Pi SD Card Linux partition