Esempio n. 1
0
        /// <summary>
        /// This method list all the records in Users.dat
        /// </summary>
        /// <param></param>
        /// <returns>A list of all Authors in the file/returns>
        public static List <AuthorProduct> ListAllRecords()
        {
            List <AuthorProduct> allAuthors = new List <AuthorProduct>();

            if (File.Exists(filePath))
            {
                using (StreamReader sr = new StreamReader(filePath))
                {
                    // read the first line in the file;
                    string line = sr.ReadLine();
                    while (line != null)
                    {
                        //split the line to get the Id
                        string[]      fields   = line.Split(',');
                        AuthorProduct anAuthor = new AuthorProduct();
                        anAuthor.AuthorId  = Convert.ToInt32(fields[0]);
                        anAuthor.ProductId = Convert.ToInt32(fields[1]);
                        allAuthors.Add(anAuthor);

                        // read the next line
                        line = sr.ReadLine();
                    }
                }
            }
            else
            {
                MessageBox.Show("File not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(allAuthors);
        }
Esempio n. 2
0
        /// <summary>
        ///  This function updates the fields of an object AuthorProduct
        /// </summary>
        /// <param name="anAuthor"></param>
        /// <returns>True if the update was succesfull; False otherwise</returns>
        public static bool Update(AuthorProduct anAuthor)
        {
            if (File.Exists(filePath))
            {
                StreamReader sr = new StreamReader(filePath);
                StreamWriter sw = new StreamWriter(filePath2, true);
                // read the first line
                string line = sr.ReadLine();
                while (line != null)
                {
                    string[] fields = line.Split(',');
                    if (anAuthor.AuthorId != Convert.ToInt32(fields[0]))
                    {
                        //write to the new file
                        sw.WriteLine(line);
                    }
                    else
                    {
                        // write the updated User to the new file
                        sw.WriteLine(anAuthor.AuthorId + "," + anAuthor.ProductId);
                    }

                    // Read the next line
                    line = sr.ReadLine();
                }

                //Close the files
                sr.Close();
                sw.Close();

                //delete the old file
                File.Delete(filePath);

                //Rename the new file with the old name
                File.Move(filePath2, filePath);
            }
            else
            {
                MessageBox.Show("File Not Found", "Error");
                return(false);
            }
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// This method search for the first occurence of an ID into the file.
        /// </summary>
        /// <param name="id"></param>
        /// <returns>If found returns an object AuthorProduct, return and object AuthorProduct set to null if the ID is not found /returns>
        public static List <AuthorProduct> SearchRecord(int id, int searchBy = 0)
        {
            int Idx = 0; //Default Search by Author ID

            if (searchBy > 0)
            {
                Idx = 1;
            }                              //If searchBy is different of author ID, force search by Book Id

            List <AuthorProduct> authorBooks = new List <AuthorProduct>();


            if (File.Exists(filePath))  // Check if the file exists beforre reading it.
            {
                using (StreamReader sr = new StreamReader(filePath))
                {
                    // read the first line in the file;
                    string line = sr.ReadLine();
                    while (line != null)
                    {
                        //split the line to get the Id
                        string[] fields = line.Split(',');

                        if (id == Convert.ToInt32(fields[Idx]))
                        {
                            // Author or Book Id found
                            AuthorProduct anAuthor = new AuthorProduct();
                            anAuthor.AuthorId  = Convert.ToInt32(fields[0]);
                            anAuthor.ProductId = Convert.ToInt32(fields[1]);
                            authorBooks.Add(anAuthor);
                        }
                        // read the next line
                        line = sr.ReadLine();
                    }
                }
            }
            else
            {
                MessageBox.Show("File not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return(authorBooks);
        }