コード例 #1
0
        private void ReadFile()
        {
            int i = 0;

            try
            {
                //to read file
                StreamReader inputFile;
                //to hold a line from the file
                string line;



                //create delimiter array
                char[] delim = { ',' };

                //open the vacations.txt file
                inputFile = File.OpenText("vacations.txt");

                //read the lines from file
                while (!inputFile.EndOfStream && i < vacations.Length)
                {
                    //create an instance of the Vacation
                    // Vacation trip = new Vacation();

                    //read a line from file
                    line = inputFile.ReadLine();

                    //tokenize the line
                    string[] tokens = line.Split(delim);

                    //store the tokens in the trip object
                    string  loc   = tokens[0];
                    int     lng   = int.Parse(tokens[1]);
                    decimal price = decimal.Parse(tokens[2]);

                    //create a new Vacation object at index
                    vacations[i] = new Vacation(loc, lng, price);

                    i++;
                }
            }
            catch (Exception ex)
            {
                //display error message
                MessageBox.Show(ex.Message);
            }
        }
コード例 #2
0
        private void ReadFile()
        {
            int i = 0;

            try
            {
                //to read file
                StreamReader inputFile;
                //to hold a line from the file
                string line;



                //create delimiter array
                char[] delim = { ',' };

                //open the vacations.txt file
                inputFile = File.OpenText("vacations.txt");

                //read the lines from file
                while (!inputFile.EndOfStream && i < vacations.Length)
                {
                    //create an instance of the Vacation
                    vacations[i] = new Vacation();

                    //read a line from file
                    line = inputFile.ReadLine();

                    //tokenize the line
                    string[] tokens = line.Split(delim);

                    vacations[i].Location = tokens[0];
                    vacations[i].Length   = int.Parse(tokens[1]);
                    vacations[i].Cost     = decimal.Parse(tokens[2]);

                    i++;
                }

                inputFile.Close();
            }
            catch (Exception ex)
            {
                //display error message
                MessageBox.Show(ex.Message);
            }
        }