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

Writing items from listbox to text file in C# with try-catch statment

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

//可变用户输入,存入userInput 尝试 { if (double.TryParse(startTextbox.Text, out start)) { 如果 (double.TryParse(averageTextbox.Text, 出平均值)) { if (double.TryParse(daysTextbox.Text, out days)) { //过程 整数计数 = 1; 而(计数 <= 天) { //计算 双输出; 输出 = 开始 * Math.Pow((1 + 平均值 / 100), 计数 - 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);
        }
        }

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

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