A
A
Artyom2022-03-18 15:02:26
C++ / C#
Artyom, 2022-03-18 15:02:26

Implementation of "Save". How to do?

using SaveFileDialog how to do "Save as" I understood, but how to implement just "Save", where let's say an open file already exists, and I just need to make changes to it. I understand that you need to somehow remember the path of the file, but in the vastness of the great Internet I still don’t understand how, since everywhere an example is given with the file path already known in advance, let’s say on drive C. Or pervert with some kind of flag, but there the problem is that all these options are described in separate Void, but I use menuStrip

The code
string FileName = "";

        void открытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string fileText;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileName = openFileDialog1.FileName;

                fileText = File.ReadAllText(FileName);
                richTextBox1.Text = fileText;
            }
        }

        void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                
            }
        }

        void сохранитьКакToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileName = saveFileDialog1.FileName;

                File.WriteAllText(FileName, richTextBox1.Text);
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom, 2022-03-18
@shindayoni

Solution:
From @twobomb
Well, in general, you don’t have the concept of an open file in the code, you read the text and closed it, there are no locks.
Why, when pressed, save the path if it is known.

void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
{
    File.WriteAllText(FileName, richTextBox1.Text);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question