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

本文关键字:文件 文本 语句 try-catch 列表 使用 | 更新日期: 2023-09-27 18:34:18

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

变量用户输入

,存储在用户输入中 尝试 { 如果(双倍。TryParse(startTextbox.Text, out start(( { 如果(双倍。TryParse(averageTextbox.Text, out average(( { 如果(双倍。TryParse(daysTextbox.Text, out days(( { 过程 整数计数 = 1; 而(计数 <= 天( { 计算 双输出; 输出 = 开始 * 数学.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);
        }
        }

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

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

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