views:

1188

answers:

4

hi I have a webservice using .net c# and I want to write to a text file on the server, but I cannot get this to work I assume it is a permission problem? thanks Stuart I think the problem is I am using System.IO.Directory.GetCurrentDirectory() what should I be using? thanks Stuart

+5  A: 

Try granting the ASP.NET user (or whatever account IIS is running as) permission to write to the folder you are trying to write to.

If this is a network share, try to run IIS as a domain user that can write to the share.

Remember the principle of granting minimal permission (dont use Admin level access).

StingyJack
I've seen this problem several times and this is almost always the correct approach to solving it - check the permissions on the IIS user and if it's a network share you're writing to, it has to be a domain user.
CMPalmer
I always forget to mention network shares. +1 :)
Kev
A: 

You should be able to write files from web services. This is most likely a permissions or trust issue. If you are in a limited trust (i.e., Medium trust) ensure that you are writing to a patch in or below your web root. If you are already doing that, or are in a full trust environment check to make sure that the directory has permissions for the IIS worker process to write to it.

Jason Whitehorn
+2  A: 

If you're running on Windows 2003 and haven't turned on ASP.NET impersonation, and are running the app in the DefaultAppPool or an application pool that is configured to run under the identity of "Network Service", then you'll need to give the "Network Service" account write permission to the destination folder. If you're running the site in an app pool that is using an identity other than "Network Service" then that account may require write permissions to the destination folder.

If you're running windows 2000 then the '<MACHINENAME>\ASPNET' account will need write permissions to the destination folder.

If you've got impersonation turned on then you'll need to give the site's anonymous user account write permissions to the destination folder instead.

To check if impersonation is turned on, open (assuming ASP.NET 2.0) then check your machine.config file (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG) to see if you have the following setting:

<identity impersonate="true"/>

This may also be overridden in your application's web.config.

Additionally if you're running in a partial trust environment then you'll likely only be able to write to the website's application folder because the default FileIOPermission is usually set to $AppDir$, i.e. you can't modify files anywhere else, even with the correct NTFS permissions.

If you're writing to a network share then StingyJack has the answer for you, but the partial trust environment considerations still apply.

But check your NTFS perms first, that's probably the best bet.

Hope this helps Kev

Kev
+1  A: 

Stuart, instead of using System.IO.Directory.GetCurrentDirectory(), you may want to use Server.MapPath. You can give Server.MapPath the directory relative to the location of your web service where you want to save the file, otherwise you probably need to pass a full file path "C:\Files\file.txt". As far as the permissions issue, I am usually able to resolve this by adding write access to the folder I'm writing the file to for IIS_WPG and ASPNET users (These usernames may be different on your server).

Austin