Search code examples
c#editingprocess.start

How to fix "used by another process" error when editing a text file which was opened using Process.Start()?


I' making a console application in c#. I used a Process.Start() function to open a text file, which can be edited directly in notepad and program waits for the notepad to close. But when trying to save the file, a warning message pops up saying "File being used by another process".

Since I'm a beginner, I don't know how to fix this problem. I know about FileAccess, FileMode, FileStream but I have never used them and don't think they will help at this point.

This is the constructor before Main() method

public AccountApplication()
        {
            Process process = Process.Start("notepad.exe", textFilePath);
            process.WaitForExit();
        }

And part of the Main() method where this constructor is used

           TextFileWriting:
                AccountApplication openFile;
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.Write("Enter a file name: ");
                string filename = Console.ReadLine();
                if (filename == "")
                {
                    goto TextFileWriting;
                }
                textFilePath = Path.Combine(currentUserFolder, filename + ".txt");

                if (File.Exists(textFilePath))
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("File You specified already has been created. Do you want to overwrite or edit it? (edit/overwrite)");
                    string userAnswer = Console.ReadLine();
                    if (userAnswer == "edit")
                    {
                        openFile = new AccountApplication();
                        goto MainMenu;
                    }
                    else if (userAnswer == "overwrite")
                    {

                        File.CreateText(textFilePath);
                        openFile = new AccountApplication();
                        goto MainMenu;
                    }
                    else if (userAnswer == "")
                    {
                        goto TextFileWriting;
                    }

                }
                else if (!File.Exists(textFilePath))
                {
                    File.CreateText(textFilePath);
                    openFile = new AccountApplication();
                    goto MainMenu;
                }

Notepad opened and the program was waiting for it to close. One thing user was unable to do is to save the changes made.


Solution

  • The line below creates a StreamWriter for you:

    File.CreateText(textFilePath);
    

    It is intended to be used like this:

    using (var writer = File.CreateText(textFilePath))
    {
        writer.WriteLine("Hi!");
    };
    

    If you don't want to write anything to the file, just close the StreamWriter immediately:

    File.CreateText(textFilePath).Close();