-1

我正在编写的程序用于注册设置。它可以很好地写入文本文件,但我想确保如果它没电并切断(平板电脑)它会保存文档。它第一次工作,但此后没有。

这是我的代码:

public void Form1_Load(object sender, EventArgs e)
{            
    this.TopMost = true;
    this.FormBorderStyle = FormBorderStyle.None;
    this.WindowState = FormWindowState.Maximized;
    textBox1.Select();
    var fileSave = new FileStream(fullFileName, FileMode.Create);
    fileSave.Close();
    //     DisableCloseButton(); 
}

private void textBox1_TextChanged_1(object sender, EventArgs e)
{
    // SqlConnection sqlConnection1 = new SqlConnection(
    //         "Data Source=DATABASE;Initial Catalog=imis;Integrated Security=True");
    //  SqlCommand cmd = new SqlCommand();

    Object returnValue;

    string txtend = textBox1.Text;
    try
    {
        string lastTwoChars = txtend.Substring(txtend.Length - 1);

        returnValue = textBox1.Text.Replace(@"*", "");

        if (lastTwoChars != "*") return;
        {
            if (listBox1.Items.Contains(returnValue))
            {
                for (int n = listBox1.Items.Count - 1; n >= 0; --n)
                {
                    string removelistitem = returnValue.ToString();
                    if (listBox1.Items[n].ToString().Contains(removelistitem))
                    {
                        //listBox1.Items.RemoveAt(n);
                    }
                }
            }
            else
                listBox1.Items.Add(returnValue);

            textBox1.Text = null;
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fullFileName);
            foreach (object item in listBox1.Items)
                sw.WriteLine(item.ToString());

            sw.Close();
            if (listBox1.Items.Count != 0) 
            { 
                DisableCloseButton(); 
            }
            else
            {
                EnableCloseButton();
            }
            label6.Text = "Currently " + 
                 listBox1.Items.Count.ToString() + " in attendance.";
        }
    }
    catch { }

}
4

4 回答 4

1

使用using 语句确保释放所有资源。

代替

System.IO.StreamWriter sw = new System.IO.StreamWriter(fullFileName);
foreach (object item in listBox1.Items)
    sw.WriteLine(item.ToString());

sw.Close();

using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fullFileName))
{
    foreach (object item in listBox1.Items)
        sw.WriteLine(item.ToString());
}
于 2013-09-23T10:45:30.207 回答
1

您是否尝试过在关闭之前调用Stream.Flush()FileStream

于 2013-09-23T10:38:06.527 回答
0

这是我的代码中的一个工作示例,附加在文件中。

using (StreamWriter xyz = new StreamWriter(Path.Combine(File_Path, "xyz.txt"), true, Encoding.Unicode))
            {
                foreach (string item in listBox1.Items)
                {
                xyz.WriteLine("ABC"); // or whatever you want
                //xyz.WriteLine(item);
                xyz.Flush();
                }
            }

希望这可以帮助。

于 2013-09-25T10:41:50.957 回答
0

我不知道你触发问题的场景,但是这条线 if (lastTwoChars != "*") return; 会跳过下面的逻辑,所以你写的可能永远不会被调用。ps 你没有在 Close 之前主动调用 Flush。

于 2013-09-23T11:59:11.517 回答