[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-10-24
5
Medium Priority
?
1,469 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
4 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

872 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