使用try-catch语句将列表框中的项目写入C#中的文本文件

时间:2016-02-11 05:28:34

标签: c# listbox try-catch streamwriter

我的代码很长,但这是我坚持的部分。我在for循环之前的开头有我的try语句。但现在我想获取ListBox中的信息并将其发送到我已在调试文件夹中的文本文件中。我想从列表框中取出输出并将它们写入population.txt文件。

//变量用户输入,存储在userInput中             尝试             {                 if(double.TryParse(startTextbox.Text,out start))                 {                     if(double.TryParse(averageTextbox.Text,out average))                     {                         if(double.TryParse(daysTextbox.Text,out days))                         {                             //处理                             int count = 1;                             而(数< =天)                             {                                 //计算                                 双输出;                                 output = start * Math.Pow((1 + average / 100),count - 1);

                            //display the results in the listbox
                            populationListBox.Items.Add("The approximate population for " +
                                count + " day(s) is " + output.ToString("n2"));

                            //count the days
                            count = count + 1;
                        }
                        //used to text statement
                        //populationListBox.Items.Add("End of while loop");

                        count = 1;
                        do
                        {
                            //calculation
                            double output;
                            output = start * Math.Pow((1 + average / 100), count - 1);

                            //display the results in the listbox
                            populationListBox.Items.Add("The approximate population for " +
                                 count + " day(s) is " + output.ToString("n2"));

                            //count the days
                            count = count + 1;
                        } while (count <= days);
                        //used to text statement
                        //populationListBox.Items.Add("End of do-while loop");

                        //int count;
                        for (count = 1; count <= days; )
                        {
                            //calculation
                            double output;
                            output = start * Math.Pow((1 + average / 100), count - 1);

                            //display the results in the listbox
                            populationListBox.Items.Add("The approximate population for " +
                                 count + " day(s) is " + output.ToString("n2"));

                            //count the days
                            count = count + 1;
                        }
                        //used to text statement
                        //populationListBox.Items.Add("End of for loop");


                    }
                    else
                    {
                        //error message for input
                        MessageBox.Show("Invalid input for number of days to multiply.");
                    }
                }
                else
                {
                    //error message for input
                    MessageBox.Show("Invalid input for average daily increase.");
                }
            }
            else
            {
                //error message for input
                MessageBox.Show("Invalid input for starting days");
            }


            StreamWriter outputFile;
            outputFile = File.CreateText("population.txt");

            outputFile.WriteLine("Approximate population: ");
            outputFile.WriteLine(populationListBox.Items);
            outputFile.ToString();
            outputFile.Close();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        }

1 个答案:

答案 0 :(得分:1)

您可以使用FileHelper之类的库来执行您的工作。它是开源和免费的。如果你只想使用.NET框架中的FileIO,你也可以这样做

using (StreamWriter sr = File.CreateText("population.txt"))
{
      foreach (string s in listBox.Items)
      { 
          sr.WriteLine(s); 
      }
}