Пример #1
0
        /// <summary>
        /// Allows to register a new user
        /// </summary>
        /// <param name="u">User that will be registered</param>
        public void registrarUsuario(Usuario u)
        {
            if (existeUsuario(u) != true)
            {
                string path = Path.GetFullPath("usuarios.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());
                        sw.Close();
                    }
                }
                else
                {
                    using (StreamWriter file = new StreamWriter(path, true))
                    {
                        file.WriteLine(u.ToString()); //se agrega información al documento

                        file.Close();
                    }
                }
            }
            else
            {
                throw new Exception("El Usua ya se encuentra registrado!!");
            }
        }
Пример #2
0
        /// <summary>
        /// Allows to update the information of a specific user
        /// </summary>
        /// <param name="actual">UserCode</param>
        /// <param name="u">Updated Information</param>
        public void editarUsuario(string actual, Usuario u)
        {
            //Editar
            StreamReader lectura;
            StreamWriter escribir;
            string       cadena, empleado;
            bool         encontrado;

            encontrado = false;
            string[] campos    = new string[5];
            char[]   separador = { ',' };
            try
            {
                string path  = Path.GetFullPath("usuarios.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();
                }
                if (encontrado == true)
                {
                }
                else
                {
                }
                lectura.Close();
                escribir.Close();

                File.AppendAllText(patho, u.ToString() + "\n");
                File.Delete(path);
                File.Move(patho, path);
            }
            catch (FileNotFoundException fe)
            {
            }
            catch (Exception be)
            {
            }
        }