0
手順空白になり、保存されたテキストファイルの最後の入力を示していますのStreamWriterは最初の一つだけが新しい行に
私が入ると:姓、姓、ID番号、クラスおよびグレード。 「OK」を押してください。 "Load"(入力された情報は、読み取り専用TextBoxに下に表示されます)。もう一度入力してください。「OK」と「ロード」(これで2行の情報が表示されます)。
:私はどこかに私のTXTファイルを保存し、それを開き、名前を付けて保存を打つ
。それはあなたが既存のファイルにテキストを追加する方法を使用することのappendText
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace StudentGrades
{
public partial class Form1 : Form
{
static string status;
public Form1()
{
InitializeComponent();
writeButton.Enabled = false;
}
private void readButton_Click(object sender, EventArgs e)
{
using (StreamReader sr =
new StreamReader(@"C:\Users\User\Documents\Visual Studio 2017\Projects\StudentGrades\StudentGrades\TextFile.txt"))
{
textBoxReadGrades.AppendText(sr.ReadToEnd());
}
writeButton.Enabled = true;
}
private async void writeButton_Click(object sender, EventArgs e)
{
using (SaveFileDialog sfd = new SaveFileDialog() { Filter =
"TextDocuments|*txt.", ValidateNames = true})
{
if (sfd.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(sfd.FileName))
{
await sw.WriteLineAsync(textBoxFirstName.Text +
" " + textBoxLastName.Text +
" " + textBoxID.Text +
" " + textBoxClass.Text +
" " + textBoxGrade.Text);
MessageBox.Show("You have successfully saved", "Message",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
private void okButton_Click(object sender, EventArgs e)
{
using (StreamWriter sw =
new StreamWriter(@"C:\Users\User\Documents\Visual Studio 2017\Projects\StudentGrades\StudentGrades\TextFile.txt"))
sw.WriteLine(textBoxFirstName.Text +
" " + textBoxLastName.Text +
" " + textBoxID.Text +
" " + textBoxClass.Text +
" " + textBoxGrade.Text + "\n");
status = StatusLabel.Text =
"Entry Saved";
writeButton.Enabled = false;
}
private void GetInfo (string fileName)
{
textBoxReadGrades.AppendText(File.ReadAllText(fileName));
}
private void ExitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
あなたのコードは1行しか書いていません。最後に追加する場合は、次のようにします。StreamWriter sw = new StreamWriter(sfd.FileName、true) – jdweng