/// <summary> /// Clear logs. Reset the folder and file URL for later use. /// </summary> public void ClearLogs() { this.PerformBlockAndWait(() => { this.fileHandle.CloseFile(); this.fileHandle.Dispose(); this.fileHandle = null; NSFileManager.DefaultManager.Remove(this.folderUrl, out NSError error); if (error != null) { Console.WriteLine($"Failed to clear the log folder!\n{error.LocalizedDescription ?? string.Empty}"); } // Create a new file handle. this.fileUrl = null; this.folderUrl = null; this.fileHandle = NSFileHandle.OpenUpdateUrl(this.FileUrl, out NSError urlError); System.Diagnostics.Debug.Assert(this.fileHandle != null, "Failed to create the file handle!"); }); }
private void PlatformWriteFile(string path, string contents) { if (FileExists(path)) { NSFileHandle handle = NSFileHandle.OpenUpdateUrl(NSUrl.CreateFileUrl(new[] { path }), out NSError error); if (error != null) { throw new NSErrorException(error); } else { handle.SeekToEndOfFile(); handle.WriteData(NSData.FromString(contents)); handle.CloseFile(); } } else { CreateFile(path, NSData.FromString(contents)); } }
private Logger() { this.fileHandle = NSFileHandle.OpenUpdateUrl(this.FileUrl, out NSError error); System.Diagnostics.Debug.Assert(this.fileHandle != null, "Failed to create the file handle!"); }
public void WriteFile(string path, string userIdentity, string contents) { if (!string.IsNullOrEmpty(path) || !string.IsNullOrEmpty(contents)) { if (!NSFileManager.DefaultManager.FileExists(path)) { var dict = new NSMutableDictionary(); UIApplication.SharedApplication.InvokeOnMainThread(() => { NSFileManager.DefaultManager.CreateFile(path, NSData.FromString(contents), dict); }); _fileProtectionManagerService.ProtectFile(path, userIdentity); _fileProtectionManagerService.EncryptFile(path, userIdentity); } else { NSFileHandle fileHandle = null; NSError error = null; Exception exception = null; try { UIApplication.SharedApplication.InvokeOnMainThread(() => { _fileProtectionManagerService.DecryptFile(path); fileHandle = NSFileHandle.OpenUpdateUrl(NSUrl.CreateFileUrl(new[] { path }), out error); fileHandle.SeekToEndOfFile(); fileHandle.WriteData(NSData.FromString(contents)); _fileProtectionManagerService.ProtectFile(path, userIdentity); _fileProtectionManagerService.EncryptFile(path, userIdentity); }); } catch (Exception ex) { exception = ex; } finally { if (fileHandle != null) { UIApplication.SharedApplication.InvokeOnMainThread(() => { fileHandle.CloseFile(); }); } } if (error != null) { _loggingService.LogError(typeof(FileSystemService), new Exception(error.DebugDescription), error.Description); throw new NSErrorException(error); } else if (exception != null) { _loggingService.LogError(typeof(FileSystemService), exception, exception.Message); throw exception; } } } }