public string ReadFile(string path, string userIdentity) { string result = String.Empty; if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0)) { if (FileExists(path)) { NSFileHandle fileHandle = null; NSError error = null; Exception exception = null; try { UIApplication.SharedApplication.InvokeOnMainThread(() => { _fileProtectionManagerService.DecryptFile(path); fileHandle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out error); if (fileHandle != null) { var nsStringResult = NSString.FromData(fileHandle.ReadDataToEndOfFile(), NSStringEncoding.UTF8); if (nsStringResult != null) { result = nsStringResult.ToString(); } } _fileProtectionManagerService.EncryptFile(path, userIdentity); }); } catch (Exception ex) { exception = ex; } finally { if (fileHandle != null) { 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; } } } return(result); }
private Stream PlatformGetInputStream(string path) { if (FileExists(path)) { NSFileHandle handle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out NSError error); if (error != null) { throw new NSErrorException(error); } else { NSData result = handle.ReadDataToEndOfFile(); handle.CloseFile(); return result.AsStream(); } } throw new FileNotFoundException(); }
private string PlatformReadFile(string path) { if (FileExists(path)) { NSFileHandle handle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out NSError error); if (error != null) { throw new NSErrorException(error); } else { NSString result = NSString.FromData(handle.ReadDataToEndOfFile(), NSStringEncoding.UTF8); handle.CloseFile(); return result?.ToString() ?? ""; } } throw new FileNotFoundException(); }
public Stream GetInputStream(string path, string userIdentity) { NSFileHandle fileHandle = null; NSError error = null; NSData result = null; Exception exception = null; try { UIApplication.SharedApplication.InvokeOnMainThread(() => { _fileProtectionManagerService.DecryptFile(path); fileHandle = NSFileHandle.OpenReadUrl(NSUrl.CreateFileUrl(new[] { path }), out error); result = fileHandle.ReadDataToEndOfFile(); _fileProtectionManagerService.EncryptFile(path, userIdentity); }); } catch (Exception ex) { exception = ex; } finally { if (fileHandle != null) { fileHandle.CloseFile(); } } if (error != null) { throw new NSErrorException(error); } else if (exception != null) { throw exception; } return(result?.AsStream() ?? null); }