域帐户运行的Windows服务无法在完全控制时访问文件

时间:2013-09-25 11:15:16

标签: service excel-interop windowsdomainaccount

我创建了一个C#服务:

- Picks up and opens a local text file
- Opens an Excel-file used as template (saved locally)
- Fills in the data from the text file in the excel file
- Saves the Excel file to a network folder.

该服务使用域帐户运行(我无法通过网络管理员在网络上授予本地系统帐户权限...)。当服务尝试打开模板时,我收到拒绝访问错误:

Microsoft Excel cannot access the file 'C:\BloxVacation\Template\BloxTemplate.xlsm'. There are several possible reasons:

• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.

文件确实存在且路径正确。 该文件未被其他用户或程序使用。 我尝试打开工作簿(没有其他工作簿是打开的),而不是保存它。

我使用系统帐户收到了同样的错误。原因是,在使用interopservices时,系统帐户需要一个桌面文件夹(Windows 7中的错误:http://forums.asp.net/t/1585488.aspx)。

C:\Windows\System32\config\systemprofile\Desktop
C:\Windows\SysWOW64\config\systemprofile\Desktop

创建这两个文件,系统帐户的错误消失。

我已经为这些文件夹授予了域用户权限,但错误消失了,但是,服务挂起在我打开excel文件的代码行上。当我使用系统帐户执行完全相同的代码时,代码执行得很好(注意:我在本地保存文件)。

 objXL.Workbooks.Open(BloxVacationService.ExcelTemplateFilePath)

有没有人知道如何在不必重写OpenXML中的整个服务的情况下解决这个问题?非常感谢你提前。

1 个答案:

答案 0 :(得分:0)

如果你已经完成了问题中描述的所有事情,但它仍然不起作用(就像我一样),答案很简单:

在运行该服务的计算机上使域用户本地管理员。它解决了这个问题。