Я делаю консольное приложение на c #. Я использовал функцию Process.Start()
, чтобы открыть текстовый файл, который можно редактировать прямо в блокноте, и программа ожидает закрытия блокнота. Но при попытке сохранить файл появляется предупреждающее сообщение «Файл используется другим процессом».
Поскольку я новичок, я не знаю, как решить эту проблему. Я знаю о FileAccess
, FileMode
, FileStream
, но я никогда не использовал их и не думаю, что они помогут на этом этапе.
Это конструктор перед методом Main ()
public AccountApplication()
{
Process process = Process.Start("notepad.exe", textFilePath);
process.WaitForExit();
}
И часть метода Main (), где используется этот конструктор
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;
}
Блокнот открылся, и программа ожидала его закрытия. Пользователь не смог сохранить сделанные изменения.