All,
I have a ~/temp dir on my web root in the web server. Let’s say:
C:\inetpub\myapp\temp
and I have a method in the code behind class of a page that deletes all the files there. Everything works well.
protected CleanTemp() {
// clean all the files in tmp dir
Array.Foreach...
}
The problems is that if I create a timer in my Global.aspx that tries to execute such clean in the same way, the system lists the files in the temp dir but for each of them it returns an exception saying the user does not have the proper access rights:
[Global.aspx]
void Application_Start(object sender, EventArgs e) {
PhisicalTmpFolder = Server.MapPath(~/temp);
// create new timer
System.Timers.Timer timScheduledTask = new System.Timers.Timer();
timScheduledTask.Interval = 20 * 60 * 1000.0;
timScheduledTask.Enabled = true;
timScheduledTask.Elapsed += new System.Timers.ElapsedEventHandler(timScheduledTask_Elapsed);
}
void timScheduledTask_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
// clean all the files in tmp dir
Array.ForEach(
Directory.GetFiles(PhisicalTmpFolder),
delegate(string path) {
try {
File.Delete(path); i++;
} catch (Exception ex) {
if (_logger != null) _logger.Error(ex.Message, ex);
}
}
);
}
How can I tell to the timer that it must run with the same access right of the application?
Thank you in advance,
Gianpiero
I think the problem you have here is that the ASP.NET process account does not have the appropriate rights and you have user impersonation enabled so when the operation is initiated during a page request it is done so under the request user account which does have rights.
Try granting the ASP.NET process account rights to that directory.