public static bool LeerBin(string pathArchivo, out T datos) { bool retorno = false; datos = default; try { if (pathArchivo != null) { using (FileStream fileStream = new FileStream(pathArchivo, FileMode.Open)) { BinaryFormatter formatter = new BinaryFormatter(); datos = (T)formatter.Deserialize(fileStream); retorno = true; } } } catch (DirectoryNotFoundException dirEx) { ComiqueriaException exception = new ComiqueriaException("Error: Directorio no encontrado", dirEx); ArchivoTexto.Escribir(exception, true); throw exception; } catch (Exception ex) { throw new Exception("mensaje gracioso", ex); } return(retorno); }
/// <summary> /// Permite serializar a binario /// </summary> /// <param name="dato"></param> /// <returns></returns> public static bool GuardarBinario(string ruta, T dato) { bool guardoArchivo = true; FileStream fs; BinaryFormatter ser; try { fs = new FileStream(ruta, FileMode.Create); ser = new BinaryFormatter(); ser.Serialize(fs, dato); fs.Close(); } catch (DirectoryNotFoundException e) { guardoArchivo = false; ComiqueriaException comiqueriaException = new ComiqueriaException("Error: Directorio no encontrado.", e); ArchivoTexto.Escribir(comiqueriaException, true); throw comiqueriaException; } return(guardoArchivo); }
/// <summary> /// Saves a data into a xml File. /// </summary> /// <param name="path">Path to save the file.</param> /// <param name="data">Data to save into the file.</param> /// <returns>True if can save the data, otherwise returns false.</returns> public static bool XMLSave(string path, T data) { try { using (XmlTextWriter writer = new XmlTextWriter(path, Encoding.UTF8)) { XmlSerializer serial = new XmlSerializer(typeof(T)); serial.Serialize(writer, data); return(true); } } catch (ArgumentException exe) { throw new ArgumentException(exe.Message); } catch (Exception exe) { ComiqueriaException comExe = new ComiqueriaException("Ocurrió un error, contacte al administrador.", exe); ArchivoTexto.Escribir(comExe, true); throw comExe; } }
/// <summary> /// Constructor, instancia los campos de tipo lista. /// Asocia el evento de cambios en la tabla de productos para actualizar la lista. /// </summary> public Comiqueria() { /* PUNTO 4D: * Cree también un método que retorne la lista de productos (List<Producto>) almacenada en la tabla de productos. * Utilice este método para cargar la lista de productos en la clase Comiqueria cuando se instancie una nueva comiquería. */ this.ventas = new List <Venta>(); try { this.productos = ComiqueriaLogic.Entidades.ComiqueriaDAO.LeerProductos(); } catch (ComiqueriaLogic.Comun.ComiqueriaException e) { ComiqueriaException exx = new ComiqueriaException("No se agregar el producto"); ArchivoTexto.Escribir(exx.Ruta, exx.Texto, true); throw exx; } }
/// <summary> /// Reads a file from a path and returns its content. /// </summary> /// <param name="path">Path of the file.</param> /// <param name="data">Content of the file.</param> /// <returns>True if can read the file, otherwise returns false.</returns> public static bool BinnaryRead(string path, out string data) { try { using (BinaryReader reader = new BinaryReader(File.OpenRead(path))) { data = reader.ReadString(); return(true); } } catch (DirectoryNotFoundException exe) { ComiqueriaException comExe = new ComiqueriaException("Error: Directorio no encontrado", exe); ArchivoTexto.Escribir(comExe, true); throw comExe; } catch (ArgumentException exe) { throw new ArgumentException(exe.Message); } catch (Exception exe) { ComiqueriaException comExe = new ComiqueriaException("Ocurrió un error, contacte al administrador.", exe); ArchivoTexto.Escribir(comExe, true); throw comExe; } }
/// <summary> /// Reads a file from a path and returns its content. /// </summary> /// <param name="path">Path of the file.</param> /// <param name="data">Content of the file.</param> /// <returns>True if can read the file, otherwise returns false.</returns> public static bool XMLRead(string path, out T data) { try { if (File.Exists(path)) { using (XmlTextReader reader = new XmlTextReader(path)) { XmlSerializer serial = new XmlSerializer(typeof(T)); data = (T)serial.Deserialize(reader); return(true); } } } catch (DirectoryNotFoundException exe) { ComiqueriaException comExe = new ComiqueriaException("Error: Directorio no encontrado", exe); ArchivoTexto.Escribir(comExe, true); throw comExe; } catch (Exception exe) { ComiqueriaException comExe = new ComiqueriaException("Ocurrió un error, contacte al administrador.", exe); ArchivoTexto.Escribir(comExe, true); throw comExe; } data = default(T); return(false); }
public ComiqueriaException(string message, Exception innerException) : base(message, innerException) { ArchivoTexto.Escribir(this, true); }
public ComiqueriaException(string mensaje, Exception innerException, DateTime momentoError) : base(mensaje, innerException) { this.momentoError = momentoError; ArchivoTexto.Escribir(this, true); }