I am trying to create a guest book in a web application
and I keep getting the following runtime error:
Access to the
path "c:\inetpub\wwwroot\Guestbook\guestbook.txt" is
denied.
guestbook.txt is part of my solution so im not sure what
the problem is. Can anyone help me please. The code is
below: -
private void Page_Load(object sender, System.EventArgs e)
{
dataView = new DataView(new DataTable() );
}
public void FillMessageTable()
{
DataTable table = dataView.Table;
table.Columns.Add("Date");
table.Columns.Add("First Name");
table.Columns.Add("e-mail");
table.Columns.Add("Message");
//open guest book file for reading
StreamReader reader = new StreamReader(
Request.PhysicalApplicationPath +
"guestbook.txt");
char[] separator = {'\t'};
//read in line from file
string message = reader.ReadLine();
while (message != null)
{
//split the string into its four parts
string[] parts = message.Split(separator);
//load data into table
table.LoadDataRow(parts, true);
//read in one line from file
message = reader.ReadLine();
}
//update grid
dataGrid.DataSource = table;
dataGrid.DataBind();
reader.Close();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.clearbutton.Click += new System.EventHandler
(this.clearbutton_Click);
this.submitbutton.Click += new System.EventHandler
(this.submitbutton_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void submitbutton_Click(object sender,
System.EventArgs e)
{
StreamWriter guestbook =
new StreamWriter(Request.PhysicalApplicationPath +
"guestbook.txt", true);
//write new message to file
guestbook.WriteLine(
DateTime.Now.Date.ToString().Substring(0, 10) +
"\t" + nameTextBox.Text + "\t" + emailTextBox.Text
+ "\t" + messageTextBox.Text );
//clear textboxes and close stream
nameTextBox.Text = "";
emailTextBox.Text = "";
messageTextBox.Text = "";
guestbook.Close();
FillMessageTable();
}