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...