public partial class Form1 : Form
{
SaveFileDialog sfd = new SaveFileDialog();
OpenFileDialog ofd = new OpenFileDialog();
public string contents = string.Empty;
public Form1()
{
InitializeComponent();
this.Text = "Untitled";
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != contents)
{
DialogResult dr = MessageBox.Show("Do You want to save the changes made to " + this.Text, "Save", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
sfd.Title = "Save";
if (SaveFile() == 0)
return;
else
{
richTextBox1.Text = "";
this.Text = "Untitled";
}
contents = "";
}
else if (dr == DialogResult.No)
{
richTextBox1.Text = "";
this.Text = "Untitled";
contents = "";
}
else
{
richTextBox1.Focus();
}
}
else
{
this.Text = "Untitled";
richTextBox1.Text = "";
contents = "";
}
}
private int SaveFile()
{
sfd.Filter = "Text Documents|*.txt";
sfd.DefaultExt = "txt";
if (sfd.ShowDialog() == DialogResult.Cancel)
{
richTextBox1.Focus();
return 0;
}
else
{
contents = richTextBox1.Text;
if (this.Text == "Untitled")
richTextBox1.SaveFile(sfd.FileName,RichTextBoxStreamType.PlainText);
else
{
sfd.FileName = this.Text;
richTextBox1.SaveFile(sfd.FileName,RichTextBoxStreamType.PlainText);
}
this.Text = sfd.FileName;
return 1;
}
}
private void OpenFile()
{
ofd.Filter = "Text Documents|*.txt";
if (ofd.ShowDialog() == DialogResult.Cancel)
richTextBox1.Focus();
else
{
richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
this.Text = ofd.FileName;
contents = richTextBox1.Text;
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != contents)
{
DialogResult dr = MessageBox.Show("Do You want to save the changes made to " + this.Text, "Save", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
SaveFile();
OpenFile();
}
else if (dr == DialogResult.No)
{
OpenFile();
}
else
{
richTextBox1.Focus();
}
}
else
OpenFile();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFile();
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
sfd.Filter = "Text Documents|*.txt";
sfd.DefaultExt = "txt";
if (sfd.ShowDialog() == DialogResult.Cancel)
{
richTextBox1.Focus();
}
else
{
contents = richTextBox1.Text;
richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);
this.Text = sfd.FileName;
}
}
Когда мы открываем приложение Windows notepad, открываем файл, изменяем его содержимое и сохраняем его, он просто сохраняется без открытия диалогового окна «Сохранить файл». Но в программе блокнота, которую я создал выше, открывается диалоговое окно «Сохранить файл» при нажатии «Сохранить» после изменения содержимого сохраненного файла. Хотя одно и то же имя файла появляется в диалоговом окне сохранения файла, но при нажатии «Сохранить» появляется сообщение «То же имя файла уже существует. Вы хотите его заменить?». Это то, что я хочу удалить и сделать измененное содержимое сохранено непосредственно в открывшемся файле, не открывая диалог сохранения файла.Надлежащее функционирование «Сохранить файл» в программе «Блокнот» в C#
Извините, но это не решает мою проблему. Я хочу, чтобы содержимое, измененное в уже открытом файле, было сохранено в файле, не открывая диалоговое окно «Сохранить файл» при нажатии кнопки «Сохранить». – ash
Но спасибо для идеи streamwriter .. Я получил смысл и сделал работу правильной .. – ash