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

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");
    try
    {
        Directory.CreateDirectory(tempFolder);
        Directory.CreateDirectory(extractedPath);
        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);
        myFile.SaveBinary(File.ReadAllBytes(extractedTemplate));
    }
    catch
    {
        throw;
    }
    finally
    {
        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=1.0.0.0, 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...

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
    
I checked, the folder is empty. But you are right, a bit of randomness can avoid other issues. –  Steve B Jul 9 '12 at 13:41
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.