/// <summary> /// Allows to register a new bus /// </summary> /// <param name="t">Object type Unidad which will be registered</param> public void registrarUnidad(Unidad u) { if (existeUnidad(u) != true) { //string path = @"C:\Users\Usuario\Desktop\lol.txt"; string path = Path.GetFullPath("unidades.txt");//para agregar carpetas afuera agrego ..\\ if (!File.Exists(path)) { // Create a file to write to. using (StreamWriter sw = File.CreateText(path)) { sw.WriteLine(u.ToString()); } } else { using (StreamWriter file = new StreamWriter(path, true)) { file.WriteLine(u.ToString()); //se agrega información al documento file.Close(); } } } else { throw new Exception("Ya existe la Unidad"); } }
/// <summary> /// Allows to update information of a specific bus /// </summary> /// <param name="actual">code of the bus </param> /// <param name="u">Updated information of the bus</param> public void editarUnidades(string actual, Unidad u) { StreamReader lectura; StreamWriter escribir; string cadena, empleado; bool encontrado; encontrado = false; string[] campos = new string[13]; char[] separador = { ',' }; try { string path = Path.GetFullPath("unidades.txt"); //para agregar carpetas afuera agrego ..\\ string patho = Path.GetFullPath("temp.txt"); //para agregar carpetas afuera agrego ..\\ lectura = File.OpenText(path); //escribir = File.CreateText(@"C:\Users\Usuario\Desktop\temp.txt"); escribir = File.CreateText(patho); cadena = lectura.ReadLine(); while (cadena != null) { campos = cadena.Split(separador); if (campos[0].Trim().Equals(actual)) { encontrado = true; } else { escribir.WriteLine(cadena); } cadena = lectura.ReadLine(); } lectura.Close(); escribir.Close(); File.AppendAllText(patho, u.ToString() + "\n"); File.Delete(path); File.Move(patho, path); } catch (FileNotFoundException fe) { } catch (Exception be) { } }