BKChedlia
BKChedlia

Reputation: 337

c# Access denied to create a new file

I want to create an xml file. I am the owner of the folder which will contain the created file. My code creates an empty file and when I debug I get an access denied error for this line

 Stream writeOutput = File.Create(Path.Combine(outputLocation, string.Format(FILEFORMAT, agent, Number, FILE_EXTENSIONS.XML)))

Help please

Here is my code:

SPSecurity.RunWithElevatedPrivileges(delegate()
    {

        using (Stream writeOutput = File.Create(Path.Combine(outputLocation, string.Format(FILEFORMAT, agent, Number, FILE_EXTENSIONS.XML))))
              {
                 using (Stream xmlOutput = OutputFileCreator.CreateOutputXMLFile(Number, rejectionReason))
                       {
                         byte[] buffer = new byte[xmlOutput.Length];
                         xmlOutput.Position = 0;
                         xmlOutput.Read(buffer, 0, buffer.Length);
                         writeOutput.Write(buffer, 0, buffer.Length);
                       }
              }    
    });

enter image description here

Upvotes: 1

Views: 1966

Answers (2)

ListenFirst
ListenFirst

Reputation: 139

What is the Application Pool Identity that you are running this as?

SPSecurity.RunWithElevatedPrivileges(delegate()

Causes that code block run under the App Pool identity that has site collection administrator privileges. Does that identity have the appropriate permissions?

See here:

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

Upvotes: 2

justiceorjustus
justiceorjustus

Reputation: 1965

You may have to make your machine have access to the folder, not just your user. Try adding sharing permissions to everyone in the properties for the folder or YourMachineName$ to indicate your physical machine has access.

Upvotes: 1

Related Questions