我正在编写的程序用于注册设置。它可以很好地写入文本文件,但我想确保如果它没电并切断(平板电脑)它会保存文档。它第一次工作,但此后没有。
这是我的代码:
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 { }
}