The code below gives me three compiler errors. They are as follows:
- The type or namespace name ‘TextWriter’ could not be found (are you missing a using directive or an assembly reference?)
- The name ‘File’ does not exist in the current context
- The name ‘filename’ does not exist in the current context
Can anyone provide some clarification as to why these errors are occurring, and how I can fix them?
private void button1_Click(object sender, EventArgs e)
{
using (TextWriter writer = File.CreateText(filename.txt)) {
writer.WriteLine("First name: {0}", textBox1.Text);
writer.WriteLine("Last name: {0}", textBox2.Text);
writer.WriteLine("Phone number: {0}", maskedTextBox1.Text);
writer.WriteLine("Date of birth: {0}", textBox4.Text);
}
}
For Error 1 and Error 2, make sure you are
using System.IO;For Error 3, if the name of the file to be read is “filename.text”, make sure you place the quotes like so:
because otherwise it is trying to find a variable
filenameand propertytxtrather than the string “filename.txt”.