Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

in SharePoint 2010, I created a DLL that I deploy to the GAC. From this assembly, I try to save a file then access the file I just saved in a temporary location, such as C:\temp. Everytime I try to do so, I get:

Access to the path 'C:\temp' is denied.

So I go to this folder on the server, and add Everyone to the security settings and give Everyone Full Control as a test; same result.

Any ideas what could be causing this? My DLL is correctly referenced in the Web.Config:

<SafeControl Assembly="My_SharePoint_Namespace", Version=1.0.0.0, Culture=neutral, PublicKeyToken="abcdefghijklmnop" Namespace="My_SharePoint_Namespace" TypeName="*" Safe="True" SafeAgainstScript="True" />

Any ideas why I would be getting this error?

using (FileStream stream = new FileStream(file_info.Directory.FullName, FileMode.Create, FileAccess.ReadWrite))
{
    stream.Write(fileBytes, 0, fileBytes.Length);
}
share|improve this question
    
I am very sorry for the long wait, but I failed to share one piece of information; the SP instance is running on Windows 7. We moved it to an actual server, but I haven't been able to test this. I will re-visit this issue, so please don't think I didn't read your answers. Thanks for your patience. –  Tomaszewski Jul 13 '12 at 17:44
add comment

2 Answers 2

If your site uses Anonymous access, this could be the issue as the Anonyous ID is not included in 'Everyone'. You might need to wrap your code in the RunWithElevatedPrivileges() command so that it runs under the ID of the application pool

SPSecurity.RunWithElevatedPrivileges(delegate() {
  using (SPSite site = new SPSite("siteUrlOrID")) {
    using (SPWeb web = site.OpenWeb("webUrlOrID")) {
      ... other code to access the file bytes ... 
      using (FileStream stream = new FileStream(file_info.Directory.FullName, FileMode.Create, FileAccess.ReadWrite))
      {
        stream.Write(fileBytes, 0, fileBytes.Length);
      }
    }   
  }
});
share|improve this answer
    
Hi, very good tip, I did not know that Anonymous was not part of Everyone. However, Anonymous is disabled in this case. I did try your solution anyway, but sadly it did not work. –  Tomaszewski Jun 1 '12 at 21:13
    
Did you use the SPSecurity.RunWithElevatedPrivileges() call as shown above - with ALL of your code inside the block including the retrieval of the SPSite object? If you are using SPContext.Current.Site then it is the context of the user, not the appPool, regardless of where you use it so it is important that you fetch a new SPSite object within the ElevatedPrivs block. –  Dave Wise Jun 1 '12 at 22:00
add comment

By default, FileStream needs ReadWrite access that’s why System.UnauthorizedAccessException is thrown because on a production machine, User account under which asp.net worker process runs or a windows service or for that matter any process will not have the write access to a file by default.

Please make sure you get the reference to any web is constructed INSIDE the SPSecurity.RunWithElevatedPrivileges- delegate. I'm quite sure that this will solve your problem!

You can look in Debug-mode which user is set within the web when you are inside the delegate (Property: SPWeb.CurrentUser).

Also make sure that, which user account is your ApplicationPool identity? Have you granted permissions on your folder to this account?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.