public static List<Product> GetProducts() { // if the directory doesn't exist, create it if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); // create the object for the input stream for a binary file BinaryReader binaryIn = new BinaryReader( new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)); // create the array list List<Product> products = new List<Product>(); // read the data from the file and store it in the List<Product> while (binaryIn.PeekChar() != -1) { Product product = new Product(); product.Code = binaryIn.ReadString(); product.Description = binaryIn.ReadString(); product.Price = binaryIn.ReadDecimal(); products.Add(product); } // close the input stream for the binary file binaryIn.Close(); return products; }
public static List<Product> GetProducts() { // if the directory doesn't exist, create it if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); // create the object for the input stream for a text file StreamReader textIn = new StreamReader( new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)); // create the list List<Product> products = new List<Product>(); // read the data from the file and store it in the list while (textIn.Peek() != -1) { string row = textIn.ReadLine(); string[] columns = row.Split('|'); Product product = new Product(); product.Code = columns[0]; product.Description = columns[1]; product.Price = Convert.ToDecimal(columns[2]); products.Add(product); } // close the input stream for the text file textIn.Close(); return products; }
private void btnSave_Click(object sender, System.EventArgs e) { if (IsValidData()) { product = new Product(txtCode.Text, txtDescription.Text, Convert.ToDecimal(txtPrice.Text)); this.Close(); } }
public static List<Product> GetProducts() { // create the list List<Product> products = new List<Product>(); // create the XmlReaderSettings object XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; settings.IgnoreComments = true; // create the XmlReader object XmlReader xmlIn = XmlReader.Create(Path, settings); // read past all nodes to the first Product node if (xmlIn.ReadToDescendant("Product")) { // create one Product object for each Product node do { Product product = new Product(); xmlIn.ReadStartElement("Product"); product.Code = xmlIn.ReadElementContentAsString(); product.Description = xmlIn.ReadElementContentAsString(); product.Price = xmlIn.ReadElementContentAsDecimal(); products.Add(product); } while (xmlIn.ReadToNextSibling("Product")); } // close the XmlReader object xmlIn.Close(); return products; }