Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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