写入文本文件

时间:2014-03-06 11:19:10

标签: c# text-files streamwriter

对此完全陌生。根据我单击的事件按钮,我需要写入相应的文本文件。但是,输入数据会写入同一文本文件。如何指定它写入并保存的相应文本文件。

除了文本文件的名称之外,我目前正在使用的编码是:

private void btnItemAdd_Click(object sender, EventArgs e)
{
    string sItem;
    string sNumber;

    if (rdoDrinks.Checked == false && rdoConfectionary.Checked == false)
    {
        //Message to remind user to select category
        MessageBox.Show("Please select a category");
        txtItem.Focus();
    }
    {
        {
            if ((rdoDrinks.Checked == true) && (txtItem.Text != "") && (txtItemNumber.Text == ""))

                //Message to remind user to enter a number
                MessageBox.Show("Please input a number");
                txtItemNumber.Focus();

            if ((txtItem.Text != "") && (txtItemNumber.Text != ""))
            {
                //add Item to end of list
                lstItems.Items.Add(string.Format("{0, -15} {1, -20}", txtItem.Text, txtItemNumber.Text));
                txtCount.Text = lstItems.Items.Count.ToString();
                //set focus to the text box
                txtItem.Focus();

                StreamWriter sw = File.AppendText("Drinks.txt");
                {
                    sItem = txtItem.Text;
                    sw.WriteLine(sItem);

                    sNumber = txtItemNumber.Text;
                    sw.WriteLine(sNumber);

                }

                MessageBox.Show("Details have been saved");
                txtItem.Clear();
                txtItemNumber.Clear();

                sw.Close();
            }
            else if ((rdoConfectionary.Checked == true) && (txtItem.Text != "") && (txtItemNumber.Text == ""))
                //Message to remind user to enter a number
                MessageBox.Show("Please input a number");
                txtItemNumber.Focus();

            if ((txtItem.Text != "") && (txtItemNumber.Text != ""))
            {
                //add Item to end of list
                lstItems.Items.Add(string.Format("{0, -15} {1, -20}", txtItem.Text, txtItemNumber.Text));
                txtCount.Text = lstItems.Items.Count.ToString();
                //set focus to the text box
                txtItem.Focus();

                StreamWriter sw = File.AppendText("Confectionary.txt");
                {
                    sItem = txtItem.Text;
                    sw.WriteLine(sItem);

                    sNumber = txtItemNumber.Text;
                    sw.WriteLine(sNumber);
                }

                MessageBox.Show("Details have been saved");
                txtItem.Clear();
                txtItemNumber.Clear();

                sw.Close();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您有几个问题:

  1. 您在第一个if之后缺少其他人。

  2. 以下代码永远不会最终成为true

  3. 两个if语句冲突(txtItemNumber.Text比较是问题)。因此,你永远不会写到Drinks.txt:

    if ((rdoDrinks.Checked == true) && (txtItem.Text != "") && (txtItemNumber.Text == ""))
    
    ...
    
    if ((txtItem.Text != "") && (txtItemNumber.Text != ""))