Solved

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

Posted on 2006-10-24
5
1,407 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

15 Experts available now in Live!

Get 1:1 Help Now