N
N
noytmant2021-10-30 13:57:28
C++ / C#
noytmant, 2021-10-30 13:57:28

How to access the file?

I see this for the first time. In this one I have the following code:

string path = @"text.txt";

        private void Create()
        {
            if (!File.Exists(path))
            {
                File.Create(path);
            }
        }

        private void Load()
        {
            string allText = File.ReadAllText(path);
        }


When starting the form, we check the existence of the file, if it is missing, then we create it.
After the Create method, the Load method is called
. The line takes over the entire contents of this file, at the moment it is empty. But at the same time, I cannot get it, because I get the following error - The process cannot access the file "text.txt", because this file is being used by another process.
When you restart the program, it is logical that the file already exists and the Load method works without problems. I get this error only when I create a file and then try to take something from there. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Satisfied IT, 2021-10-30
@noytmant

File.Create(path);and closing of the file after creation where? Therefore, it is used by another process.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question