Solved

System.UnauthorizedAccessException: Access to the path "..." is denied - stumped!

Posted on 2006-10-24
5
1,411 Views
Last Modified: 2007-12-19
It's the strangest thing. In ASP.NET (on Win2k3), when I try to overwrite an existing file, I always get an UnauthorizedAccessError. Here's the code:


    <WebMethod()> _
    Public Function Save_File() As String
        Dim f As New System.IO.StreamWriter("C:\inetpub\fms4net\files\asdf.txt")
        f.Write("hello world")
        f.Close()
        Return "ok"
    End Function


When I call Save_File() it the first time (the file isn't yet created), no dramas. When I call it the second time, I get the following error:

System.UnauthorizedAccessException: Access to the path &quot;C:\inetpub\fms4net\files\asdf.txt&quot; is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String str)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   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)
   at FMS4NET.Test.Save_File() in C:\Documents and Settings\Administrator\VSWebCache\....\TEST\Test.asmx.vb:line 50

I've even tried deleting the file just before writing to it, yet even the delete command fails with the same error:

System.UnauthorizedAccessException: Access to the path &quot;C:\inetpub\fms4net\files\asdf.txt&quot; is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String str)
   at System.IO.File.Delete(String path)
   at FMS4NET.Test.Save_File() in C:\Documents and Settings\Administrator\VSWebCache\....\TEST\Test.asmx.vb:line 50

The file isn't readonly. both MACHINENAME\ASPNET and MACHINENAME\IUSR_MACHINENAME have full control over both the file itself and the containing folder. (ASP.NET's creating hte file initially after all.) I have also recompiled the application (with minor changes to ensure it's actually recompiled) then tried the webmethod immediately afterwards yet it still happens.  Also tried creating the file manually myself in Windows Explorer, same problem occurs.

I feel like an idiot, lol. Any ideas?

Thanks in advance,
-blowfly
0
Comment
Question by:blowfly
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 17795466
Try

<WebMethod()> _
    Public Function Save_File() As String
        Dim f As New System.IO.StreamWriter("C:\inetpub\fms4net\files\asdf.txt")
        f.Write("hello world")
        f.Flush() '<--------------------- added
        f.Close()
        f.Dispose() '<----------------- added
        Return "ok"
    End Function
0
 

Author Comment

by:blowfly
ID: 17800000
No luck I'm afraid - Flush didn't help, and the .Dispose() member isn't available. (VS.NET2003)

Also i'm still unable to delete the file first so something more fundamental is going on. I can still delete the file through Windows Explorer.
0
 

Author Comment

by:blowfly
ID: 17878121
Found the answer - turns out the parent folder needed the MACHINENAME\Users group to have "Modify" access. Rather obvious in hindsight. Problem solved.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18171150
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now