具有表单和多个输出的文本文件

时间:2019-05-11 10:48:03

标签: c# file io output

我有一个代码,需要从用户那里获取当天(特定城市)天气的输入,并将该输入存储到文本文件中,然后从文本文件中显示特定城市的输入。我利用了FORMS。

如何更改代码以正确读取文本文件,并且不每次显示3次输入? 我在以下位置添加了使用文本文件的相关方法:

  private void btnCapture_Click(object sender, EventArgs e)
    {
        string city = cbCity.SelectedItem.ToString();
        DateTime date = dtp.Value;
        int minTemp = Convert.ToInt32(nudMinTemp.Value);
        int maxTemp = Convert.ToInt32(nudMaxTemp.Value);
        int precipitation = Convert.ToInt32(nudPrecip.Value);
        int humidity = Convert.ToInt32(nudHumid.Value);
        int windSpeed = Convert.ToInt32(nudWindSpeed.Value);
        using (StreamWriter sw = new StreamWriter("myFile.txt"))
        {
            sw.WriteLine(city + Environment.NewLine + date + 
                maxTemp + Environment.NewLine + precipitation...);
            sw.Close();
        }

        WeatherDetails w = new WeatherDetails(city, date, minTemp...)
        w.ReadData();
        MessageBox.Show("Your data has been Captured");
    }

   //Read data in WeatherDetails Class:
   public void ReadData()
    {

        StreamReader SR = new StreamReader("myFile.txt");
        while (!SR.EndOfStream)
        {
            city = SR.ReadLine();
            date = DateTime.Parse(SR.ReadLine());
            minTemp = int.Parse(SR.ReadLine());
            maxTemp = int.Parse(SR.ReadLine());
            precipitation = int.Parse(SR.ReadLine());
            humidity = int.Parse(SR.ReadLine());
            windSpeed = int.Parse(SR.ReadLine());
            WeatherDetails newOne = new WeatherD...                      
            ci.Add(newOne);
        }
        SR.Close();
        }

  private void btnDisplay_Click(object sender, EventArgs e)
    {

        city = cbCity.SelectedItem.ToString();
        DateStart = dtpStart.Value;
        DateEnd = dtpEnd.Value;

        //Displaying data to RTB
         private void btnDisplay_Click(object sender, EventArgs e)
    {

        city = cbCity.SelectedItem.ToString();
        DateStart = dtpStart.Value;
        DateEnd = dtpEnd.Value;

        //Displaying data to RTB
        foreach (WeatherDetails wd in WeatherDetails.ci)
        {

            rtbDisplay.Text += ("The Weather Report for " +    cbCity.SelectedItem.ToString() + " , accesssed on " + wd.date.ToString() + "\n" +
                "\n" +
                "Maximum Temperature: " + wd.maxTemp + "\n" +
                "Minimum Temperature: " + wd.minTemp + "\n" +
                "Precipitation: " + wd.precipitation + "\n" +
                "Humidity: " + wd.humidity + "\n" +
                "Wind Speed: " + wd.windSpeed + "\n" +
                "-------------------End OF Report---------------------");

问题是它每次显示输入的数据3次。因此,如果我在5月12日和5月14日输入一个城市的数据,它将显示12次3次,并显示14次3次。

0 个答案:

没有答案