-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

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 閉じる前に積極的に Flush を呼び出さないでください。

于 2013-09-23T11:59:11.517 に答える