Avatar of Cognize

asked on 

App_Data read only settings wont stick in XP / ASP.NET - Can't access file for read / write


I'm getting an exception thrown when trying to write to a file in my App_Data folder in an ASP.NET application. Sure enough when I checked, the file was read only, so I unchecked that.

However, the read only setting won't stick. If i set to read/write, go up a directory, and then straight back to it, I find it's read only again.

How is this happening??!
StreamWriter tw = new StreamWriter( m_usersListPath, true );
// Throws exception cos of read write issue

Open in new window

Windows XPASP.NETMicrosoft IIS Web Server

Avatar of undefined
Last Comment
Avatar of Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

You can try either of both of the following:

1. Ensure that the ASP.NET user has permission to write to that folder/file.
2. Use the File class to check the attributes and if necessary set the attributes of the file before attempting to write to it.


if ((File.GetAttributes(path) & FileAttributes.ReadOnly).Equals(
            FileAttributes.ReadOnly)) {
    File.SetAttributes(path, FileAttributes.Normal);

Open in new window

Avatar of Cognize


Thanks for your reply. I had actually tried setting access permissions for ASPNET (in IIS manager). I've also tried with the code you've listed (which I really through would work), but I still get the same exception thrown:

System.UnauthorizedAccessException was unhandled by user code
  Message="Access to the path 'c:\\inetpub\\wwwroot\\FileDistributer\\UsersList\\UsersList.txt' is denied."
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
       at System.IO.StreamWriter.CreateFile(String path, Boolean append)
       at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
       at System.IO.StreamWriter..ctor(String path, Boolean append)
       at System.IO.File.AppendText(String path)
       at Security.AddNewUserToList(String user, String password) in c:\Inetpub\wwwroot\FileDistributer\App_Code\Security.cs:line 50
       at Security.CreateUser(String user, String password) in c:\Inetpub\wwwroot\FileDistributer\App_Code\Security.cs:line 35
       at LogIn.AuthenticateUser(TextBox m_UserNameTextBox, TextBox m_PassWordTextBox) in c:\Inetpub\wwwroot\FileDistributer\LogIn.aspx.cs:line 27
       at LogIn.m_LogInSubmitBtn_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\FileDistributer\LogIn.aspx.cs:line 22
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

I'm now using the following code (have created a separate class that handles the file access stuff as in your code snippet).

The path that I pass in is the fully qualified local path C:/...etc..etc..FileToChange.txt

I still think that it's really odd that when I uncheck the read only setting on the parent directory, and then check to see if they have stuck, I find it is checked again (directory is read only). Is this normal? Surely I would have noticed this before...

Any help would be appreciated.

FileAccessibility.EnsureFileIsReadWrite( m_usersListPath );
StreamWriter tw = File.AppendText( m_usersListPath );

Open in new window

Avatar of Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Cognize


Hi. Sorted the problem now. Thanks for all your help. I'm awarding points because your code for programatically setting permissions was really useful, but the problem is that I was only seeing simple file options in XP. I'm including my notes for anyone searching on this:

For asp.net to have write access to files, permissions must be set appropriatley.

Some properties can be set through IIS manager, but read write options are best set on the local machine. By default, XP shows simple file options when you view properties on a file. To allow full ownership and access configuration follow these steps:

1. Ensure you have all options available to you:

If the security tab isn't visible on any of your folder properties in XP, you may have "Use Simple File Sharing" selected in your Folder Options.

Open Explorer and go to Tools>Folder Options.

Click the "View" tab and scroll to the bottom of the listbox with the heading "Advanced Settings".

Clear the "Use Simple File Sharing" checkbox.

2. Grant access to MACHINE_NAME/ASPNET, and then edit read write access accordingly.
Avatar of Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Thanks for the points Cognize but I must point out in my first comment  ID: 22078363, I noted that the ASPNET user must have permission to write to the file.

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo