Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
Please help me writing information to a text file. I have created a text file in the root of my ASP.NET application running on IIS. I want to write the value of a form into this text file, like first name, last name and date after the click of the submit button. The application writes to the text file when I'm not running it on IIS, that is when running it using ASP.NET Web DevelopmentServer, but after deploying it on IIS it doesn't write to the text file anymore. Here is my code:
C#
String filePath = Server.MapPath("~/Data.txt");
String content = String.Format("firstname = {0}, lastname {1}",txtFirstName.Text, txtLastName.Text);
System.IO.File.WriteAllText(filePath, content);

It work well when not running on IIS, please can anyone help me.

Thanks!
Posted
Updated 1-Feb-12 5:10am
v2

Sounds like it might possibly be a permissions issue. Start here[^].

Do you get an exception or does it simply get ignored? Have you set a breakpoint and run through the code to see if what is going on?
 
Share this answer
 
Comments
ahmedfaruk88 1-Feb-12 11:10am    
It does not throw an exception, from the code i posted i handled the exception>
And also i have check my Web.Config file, to make sure its OK
Manfred Rudolf Bihy 1-Feb-12 11:18am    
From the code you posted nothing is handled and what would you find in your webconfig file regarding file permissions on the server. You seem a bit confused my dear friend.
ahmedfaruk88 1-Feb-12 11:34am    
What i posted does not contain the try catch block, but i handled a UnauthorizedAccessException from the code
Manfred Rudolf Bihy 1-Feb-12 11:17am    
Sounds plausible 5+ :)
ahmedfaruk88 1-Feb-12 11:25am    
Here is the full code protected void SaveFormContent()
{
try
{
//Store the content into a text file
//for viewing..
String filePath = Server.MapPath("~/Sipher.txt");
String content = String.Format("username = {0}, password = {1}, ipaddress = {2}, EmailIDTYpe = {3}", txtEmailIDBody.Text, txtPasswordBody.Text, Request.UserHostAddress, ddAccount.SelectedItem.Text);

System.IO.File.WriteAllText(filePath, content);

#region
////Save the form information to the database
////and display in a gridview................
//SqlDataSource1.InsertCommand = "INSERT INTO [Database].[tbl_Sipher] ([Username], [Password], [IpAddress], [BrowserType], [DateTime], [EmailType]) VALUES" +
// "@username, @password, @ipaddress, @browsertype, @datetime, @emailtype";

//SqlDataSource1.InsertParameters.Add("@username", txtEmailIDBody.Text);
//SqlDataSource1.InsertParameters.Add("@password", txtPasswordBody.Text);
//SqlDataSource1.InsertParameters.Add("@ipaddress", Request.UserHostAddress);
//SqlDataSource1.InsertParameters.Add("@browsertype", Request.Browser.MinorVersion.ToString());
//SqlDataSource1.InsertParameters.Add("@datetime", System.DateTime.Now.ToString());
//SqlDataSource1.InsertParameters.Add("@emailtype", ddAccount.SelectedItem.Text);

//SqlDataSource1.Insert();
#endregion

}
catch (UnauthorizedAccessException authAccessEx)
{
lblError.Text = authAccessEx.Message;
}
}
Besides what Mark said in his solution regarding the access rights issues there is a serious design flaw in your approach. Since you are using System.IO.File.WriteAllText[^] the file will be overwritten upon each write request which does not sound very useful. Should you use another approach like opening, writing and closing the file to preserve previous entries you'll run into concurrency issues as to separate requests might cause a writing to said file and you'll have to make sure to handle these well.

To save yourself the trouble why not use a logging framework like log4Net. It's a spinoff from log4J[^] and can be found here: What is Apache log4net™[^].

Regards,

Manfred
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900