示例#1
0
        public void GetDataFromTextFile()
        {
            try
            {
                FileInfo stockfile = new FileInfo("StockList.txt");
                if (!stockfile.Exists)
                {
                    File.Create("StockList.txt").Dispose();
                }
                else
                {
                    FileStream   filestream   = stockfile.OpenRead();
                    StreamReader streamreader = new StreamReader(filestream);
                    while (!streamreader.EndOfStream)
                    {
                        string[]  line     = streamreader.ReadLine().Split(",");
                        string    code     = line[0];
                        string    name     = line[1];
                        float     price    = float.Parse(line[2]);
                        int       quantity = Int32.Parse(line[3]);
                        StockItem item     = new StockItem(code, name, price, quantity);
                        StockMgr.Items.Add(item);
                    }
                    streamreader.Close();
                }

                FileInfo logfile = new FileInfo("LogFile.txt");
                if (!logfile.Exists)
                {
                    File.Create("LogFile.txt").Dispose();
                }
                else
                {
                    FileStream   filestream   = logfile.OpenRead();
                    StreamReader streamreader = new StreamReader(filestream);
                    while (!streamreader.EndOfStream)
                    {
                        string[] line = streamreader.ReadLine().Split(",");
                        DateTime date = DateTime.Parse(line[0]);
                        string   code = line[1];
                        string   name = line[2];
                        string   type = line[3];
                        if (type == "Add")
                        {
                            float price = float.Parse(line[4]);
                            TransactionLogEntryAdd addentry = new TransactionLogEntryAdd(date, code, name, type, price);
                            LogMgr.TransactionLogEntries.Add(addentry);
                        }
                        else if (type == "Remove")
                        {
                            string person = line[5];
                            TransactionLogEntryRemove removeentry = new TransactionLogEntryRemove(date, code, name, type, person);
                            LogMgr.TransactionLogEntries.Add(removeentry);
                        }
                    }
                    streamreader.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(" Error: " + e.Message);
                Environment.Exit(0);
            }
        }