Если ваше приложение имеет доступ к диску C: \, файл demo.txt, скорее всего, помечен как доступный только для чтения, поскольку WriteAllText
попытается перезаписать файл.
Этот кодпоказывает исключение UnauthorizedAccessException с тем же текстом ошибки, что и при комментировании вашего вопроса и другого ответа.
static void Main(string[] args)
{
var demoTxt = new FileInfo("C:\\demo.txt");
demoTxt.Attributes |= FileAttributes.ReadOnly;
WriteAllText("should succeed");
try
{
demoTxt.Attributes |= FileAttributes.ReadOnly;
WriteAllText("should fail");
}
catch (UnauthorizedAccessException uae)
{
Debug.WriteLine(uae.ToString());
}
}
static void WriteAllText(string text)
{
// This is the button labeled "Save" in the program.
//
File.WriteAllText("C:\\demo.txt", text);
}
Для дальнейшего использования это может помочь вам предоставить дополнительную информацию для сообщений SO.
private void Btn_Save_Click(object sender, EventArgs e)
{
try
{
// This is the button labeled "Save" in the program.
//
File.WriteAllText("C:\\demo.txt", Tb_Admin.Text);
File.WriteAllText("C:\\demo.txt", Tb_Name.Text);
File.WriteAllText("C:\\demo.txt", Tb_Gender.Text);
}
catch(IoException ex)
{
//View the Output Window, copy the text to your question
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}