If you plan on doing any sort of file manipulation on a web-server using ASP.NET, you’re going to have to become intimiately familiar with the System.IO library and learn how to properly configure the permissions for the directories you would like to write to on your Windows Server ’03 or ’08 web-server. Fortunately, the process isn’t terribly difficult.
If your permissions aren’t properly set, you’ll get an error like this when you try to upload a file in ASP.NET:
System.UnauthorizedAccessException: Access to the path 'C:\files\filename.ext' 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)
at System.Net.WebClient.OpenFileInternal(Boolean needsHeaderAndBoundary, String fileName, FileStream& fs, Byte& buffer, Byte& formHeaderBytes, Byte& boundaryBytes)
at System.Net.WebClient.UploadFile(Uri address, String method, String fileName)
Here’s how to configure the permissions your files and directories to upload files using ASP.NET:
(1) Logon to your web-server locally or using remote desktop.
(2) Browse to the folder you would like to be able to upload files to, right click it, and select properties.
(3) Browse to the “Security” tab and select the “Network Service” account. If there’s no network service account present, click “Add” type in “Network Service” then click “Ok.”
(4) To give ASP.NET the ability to upload files, Check the “Write” checkbox under Allow. To give ASP.NET the permissions to modify, rename and delete files in that directory, click “Modify.” Click “OK”, and it’s easy as that. If you continue to have problems, you might want to check your IIS settings and see if reading and writing files are disabled in there.