Problems creating a file in output mode.

Gizmo001

Active member
Joined
Dec 27, 2002
Messages
33
I used the following lines (or variants thereof) to create a file in output mode:

Dim writer As StreamWriter
writer = New StreamWriter(Request.PhysicalApplicationPath & "NewFile.txt", False)

Whether the file exists or not, I receive the following runtime error:


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Access to the path "c:\inetpub\wwwroot\KICMG\WebApplication1\NewFile.txt" is denied.

The ASP.NET process is not authorized to access the requested resource. For security reasons the default ASP.NET process identity is {machinename}\ASPNET, which has limited privileges. Consider granting access rights to the resource to the ASP.NET process identity.

To grant ASP.NET write access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the "{machinename}\ASPNET" user. Highlight the ASP.NET account, and check the Write box in the Allow column.




When I attempt to follow the instructions in the last paragraph above, I do not get a Security tab in Properties. Can anyone suggest a solution to this problem? Thanks.
 
Is this on a hosted server or your own machine?

Just so you know, PhysicalApplicationPath gets the physical directory and path on the HDD.
It may be better to do something like this....


Code:
Dim sPath As String = Server.MapPath("WebApplication1")
I wrote WebApplication1 because that is the name of you project (you posted above).
 
Roddy: I tried that using the following code:

Dim sPath As String = Server.MapPath("WebApplication2")
Dim writer As StreamWriter
TextBox1.Text = sPath
writer = New StreamWriter(sPath & "NewFile1.txt", False)
writer.WriteLine("This works.")
writer.Close()


The textbox printed sPath as

c:\inetpub\wwwroot\WebApplication2\WebApplication2

However, there were no files or sub-subdirectories produced nor were any errors produced. Any idea where the file is created? Thanks.
 
Robby:

Adding the slash produces the follwoing error:

Could not find a part of the path "c:\inetpub\wwwroot\WebApplication2\WebApplication2\NewFile1.txt".


My original code leads to the correct file, but somewhow the access to open a file in the output mode is denied and I dont know how to change that.

Thanks.
 
Robby:

Thanks for your effort. My original code was fine. The change in write-access setting had to be done through the IIS menus. When I did that the original code worked fine.
 
Sorry to dig up this thread, but Im having the same problem,
only with trying to open a file for reading.

How do I change the IIS settings to allow file reading?
 
Bucky:

It has been sometime since I changed the settings. But try this:

Through control panel (>> Admin. tools >> IIS >> Local computer >> Default web site) reach the Default web site icon. Right click on the icon and click properties. Within properties, select Home Directory tab. You should be able to reset the read and write privileges for files.

Good luck. Let me know if this works.
 
I dont know if you ever got this fixed but you need to enable impersonation.

add a line similar to this in your web.config

<identity impersonate="true" userName="<userid>" password="<password>"/>
 
Back
Top