2016-06-14 120 views
1

在Sitecore中的计划任务中,我想在数据导入后删除XML文件。数据导入成功,但在删除时,我得到拒绝访问异常。Sitecore使用计划任务从服务器删除文件

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> 
System.UnauthorizedAccessException: Access to the path 'X:\..\Website\temp\import\data.xml' is denied. at 
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at 
System.IO.File.InternalDelete(String path, Boolean checkHost) at Website.Intranet.Kernel.ScheduledTasks.Scheduler.ImportData() 

这是我用来删除文件的代码。

if (File.Exists(filePath)) 
      { 
       File.SetAttributes(filePath, FileAttributes.Normal); 
       File.Delete(filePath); 

      } 
+0

确定运行应用程序,用户必须删除该位置的文件的权利? – Gatogordo

+0

您确定您在导入完成后释放文件句柄吗? –

+0

运行计划任务时是否引发此错误?或者你尝试调试它,而以某种方式运行它并抛出错误?尝试使用(新Sitecore.SecurityModel.SecurityDisabler()){@ – Anton

回答

1

确保您授予对上你的网站运行应用程序池用户身份的目标文件夹的正确权限。默认情况下,身份是Application Pool IdentityNetworkService

还要确保包含文件夹不只读

+0

但Sitecore把这个文件放在同一个身份下的那个位置? – Anton

+0

是的。内容文件夹是只读的。 – Kamran