I have a feature with a an event receiver that have to uncompress an archive (actually an Infopath Form), patch some data and recompress the archive (using Cab compression).

In order to "properly" clean up files, I have written this code :

private void FixArchive(SPWeb web, SPSite site)
    var myFile = web.GetFile("/pathtofile.xsn");

    var tempFolder = Path.Combine(Path.GetTempPath(), "tempFolder");
    var extractedPath = Path.Combine(tempFolder, "tempExtracted");
    var extractedTemplate = Path.Combine(tempFolder, myFile.Name);
    var extractor = new CabLib.Extract();
    var compactor = new CabLib.Compress();
    var manifestPath = Path.Combine(extractedPath, "manifest.xsf");
        File.WriteAllBytes(extractedTemplate, myFile.OpenBinary());
        extractor.ExtractFile(extractedTemplate, extractedPath);
        var manifestContent = File.ReadAllText(manifestPath);

        // Patch the file

        File.WriteAllText(manifestPath, newContent);
        compactor.CompressFolder(extractedPath, extractedTemplate, "*.*", true, true, 0);
        if (Directory.Exists(tempFolder)) Directory.Delete(tempFolder, true);

this code is working on dev computer, but not on production server.

In the ULS Logs I can see :

Feature receiver assembly 'MyProject, Version=, Culture=neutral, PublicKeyToken=c02e9cbf6dab9cb0', class 'MyProject.MyEventReceiver', method 'FeatureActivated' for feature 'bc8d119e-6308-4292-9cab-f381ee9b35fe' threw an exception: System.UnauthorizedAccessException: Access to the path 'C:\Users\myserviceaccount\AppData\Local\Temp\tempFolder' is denied.

How is it possible that a service account can't create in its own temporary folder?

Is it the correct place to deal with temp files?

PS: of course, I check that the service account has full control on its temp directory

[EDIT] I don't why, but if I specify another inexisting folder (c:\test for example) instead of Path.GetTempPath(), the code works as expected...

2 Answers 2


To solve my issue, I have to specify another folder than the %temp% directory. I'm suspecting a weird setting in the user permission somewhere in the folder hirearchy to the %temp% leaf directory.


It is possible that there is already a lock on the folder if it didn't finish successfully on a previous run as you are always using the same folder name. Try generating a new folder name using a Guid for example and clean that up so each time the code runs it is dealing with a unique folder.

  • 1
    I checked, the folder is empty. But you are right, a bit of randomness can avoid other issues.
    – Steve B
    Jul 9, 2012 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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