static void FileExistsExample() { FILINFO fno = new FILINFO(); res = Ff.Current.f_stat("/sub1/File2.txt", ref fno); switch (res) { case Ff.FRESULT.FR_OK: Debug.WriteLine($"Size: {fno.fsize}"); Debug.WriteLine(String.Format("Timestamp: {0}/{1}/{2}, {3}:{4}", (fno.fdate >> 9) + 1980, fno.fdate >> 5 & 15, fno.fdate & 31, fno.ftime >> 11, fno.ftime >> 5 & 63)); Debug.WriteLine(String.Format("Attributes: {0}{1}{2}{3}{4}", (fno.fattrib & AM_DIR) > 0 ? 'D' : '-', (fno.fattrib & AM_RDO) > 0 ? 'R' : '-', (fno.fattrib & AM_HID) > 0 ? 'H' : '-', (fno.fattrib & AM_SYS) > 0 ? 'S' : '-', (fno.fattrib & AM_ARC) > 0 ? 'A' : '-')); break; case Ff.FRESULT.FR_NO_FILE: Debug.WriteLine("File does not exist"); break; default: Debug.WriteLine($"An error occured. {res.ToString()}"); break; } }
static void MountDrive() { res = Ff.Current.f_mount(ref fs, "", 1); /* Give a work area to the default drive */ res.ThrowIfError(); Debug.WriteLine("Drive successfully mounted"); }
static void DeleteFileExample() { res = Ff.Current.f_unlink("/sub1/File2.txt"); /* Give a work area to the default drive */ res.ThrowIfError(); Debug.WriteLine("File successfully deleted"); }
static void RenameFileExample() { /* Rename an object in the default drive */ res = Ff.Current.f_rename("/sub1/File1.txt", "/sub1/File2.txt"); res.ThrowIfError(); Debug.WriteLine("File successfully renamed"); }
static void ListDirectoryExample() { res = Ff.Current.f_mount(ref fs, "", 1); res.ThrowIfError(); res = Scan_Files("/"); res.ThrowIfError(); Debug.WriteLine("Directories successfully listed"); }
static void ReadFileExample() { if (Ff.Current.f_open(ref Fil, "/sub1/File1.txt", FA_READ) == Ff.FRESULT.FR_OK) { /* Create a file */ var newPayload = new byte[5000]; res = Ff.Current.f_read(ref Fil, ref newPayload, 5000, ref bw); /* Read data from file */ res.ThrowIfError(); var msg = Encoding.UTF8.GetString(newPayload, 0, (int)bw); Debug.WriteLine($"{msg}"); res = Ff.Current.f_close(ref Fil); /* Close the file */ res.ThrowIfError(); } Debug.WriteLine("File successfully read"); }
static void CreateFileExample() { if ((res = Ff.Current.f_open(ref Fil, "/sub1/File1.txt", FA_WRITE | FA_CREATE_ALWAYS)) == Ff.FRESULT.FR_OK) { /* Create a file */ Random rnd = new Random(); var payload = $"File contents is: It works ({rnd.Next()})!".ToByteArray(); res = Ff.Current.f_write(ref Fil, payload, (uint)payload.Length, ref bw); /* Write data to the file */ res.ThrowIfError(); res = Ff.Current.f_close(ref Fil); /* Close the file */ res.ThrowIfError(); } else { res.ThrowIfError(); } Debug.WriteLine("File successfully created"); }
private static FRESULT Scan_Files(string path) { FRESULT res; FILINFO fno = new FILINFO(); DIR dir = new DIR(); byte[] buff = new byte[256]; buff = path.ToNullTerminatedByteArray(); res = Ff.Current.f_opendir(ref dir, buff); /* Open the directory */ if (res == FRESULT.FR_OK) { for (; ;) { res = Ff.Current.f_readdir(ref dir, ref fno); /* Read a directory item */ if (res != FRESULT.FR_OK || fno.fname[0] == 0) { break; /* Break on error or end of dir */ } if ((fno.fattrib & AM_DIR) > 0 && !((fno.fattrib & AM_SYS) > 0 || (fno.fattrib & AM_HID) > 0)) { /* It is a directory */ var newpath = path + "/" + fno.fname.ToStringNullTerminationRemoved(); Debug.WriteLine($"Directory: {path}/{fno.fname.ToStringNullTerminationRemoved()}"); res = Scan_Files(newpath); /* Enter the directory */ if (res != FRESULT.FR_OK) { break; } } else { /* It is a file. */ Debug.WriteLine($"File: {path}/{fno.fname.ToStringNullTerminationRemoved()}"); } } Ff.Current.f_closedir(ref dir); } return(res); }
static void GetFreeSpaceExample() { uint fre_clust = 0; uint fre_sect, tot_sect; /* Get volume information and free clusters of drive 1 */ res = Ff.Current.f_getfree("0:", ref fre_clust, ref fs); if (res != FRESULT.FR_OK) { Debug.WriteLine($"An error occured. {res.ToString()}"); return; } ; /* Get total sectors and free sectors */ tot_sect = (fs.n_fatent - 2) * fs.csize; fre_sect = fre_clust * fs.csize; /* Print the free space (assuming 512 bytes/sector) */ Debug.WriteLine(String.Format("{0} KB total drive space\n{1} KB available", tot_sect / 2, fre_sect / 2)); }
static void CreateDirectoriesExample() { res = Ff.Current.f_mkdir("sub1"); if (res != FRESULT.FR_EXIST) { res.ThrowIfError(); } res = Ff.Current.f_mkdir("sub1/sub2"); if (res != FRESULT.FR_EXIST) { res.ThrowIfError(); } res = Ff.Current.f_mkdir("sub1/sub2/sub3"); if (res != FRESULT.FR_EXIST) { res.ThrowIfError(); } Debug.WriteLine("Directories successfully created"); }
public static void ThrowIfError(this FRESULT res) { string msg; if (res != FRESULT.FR_OK) { switch (res) { case FRESULT.FR_OK: msg = "FRESULT.FR_OK"; break; case FRESULT.FR_DISK_ERR: msg = "FRESULT.FR_DISK_ERR"; break; case FRESULT.FR_INT_ERR: msg = "FRESULT.FR_INT_ERR"; break; case FRESULT.FR_NOT_READY: msg = "FRESULT.FR_NOT_READY"; break; case FRESULT.FR_NO_FILE: msg = "FRESULT.FR_NO_FILE"; break; case FRESULT.FR_NO_PATH: msg = "FRESULT.FR_NO_PATH"; break; case FRESULT.FR_INVALID_NAME: msg = "FRESULT.FR_INVALID_NAME"; break; case FRESULT.FR_DENIED: msg = "FRESULT.FR_DENIED"; break; case FRESULT.FR_EXIST: msg = "FRESULT.FR_EXIST"; break; case FRESULT.FR_INVALID_OBJECT: msg = "FRESULT.FR_INVALID_OBJECT"; break; case FRESULT.FR_WRITE_PROTECTED: msg = "FRESULT.FR_WRITE_PROTECTED"; break; case FRESULT.FR_INVALID_DRIVE: msg = "FRESULT.FR_INVALID_DRIVE"; break; case FRESULT.FR_NOT_ENABLED: msg = "FRESULT.FR_NOT_ENABLED"; break; case FRESULT.FR_NO_FILESYSTEM: msg = "FRESULT.FR_NO_FILESYSTEM"; break; case FRESULT.FR_MKFS_ABORTED: msg = "FRESULT.FR_MKFS_ABORTED"; break; case FRESULT.FR_TIMEOUT: msg = "FRESULT.FR_TIMEOUT"; break; case FRESULT.FR_LOCKED: msg = "FRESULT.FR_LOCKED"; break; case FRESULT.FR_NOT_ENOUGH_CORE: msg = "FRESULT.FR_NOT_ENOUGH_CORE"; break; case FRESULT.FR_TOO_MANY_OPEN_FILES: msg = "FRESULT.FR_TOO_MANY_OPEN_FILES"; break; case FRESULT.FR_INVALID_PARAMETER: msg = "FRESULT.FR_INVALID_PARAMETER"; break; default: msg = "FRESULT.UNDEFINED"; break; } throw new ApplicationException($"Error: {msg}"); } }