问题是,我的.NET Windows服务(在NT AUTHORITYSYSTEM
下运行)在ProgramData
内部的文件夹中创建一个文件,如果Windows应用程序试图覆盖它,它会得到一个System.UnauthorizedAccessException
。当前登录的用户具有管理员帐户,但该应用程序无法启动提升(我想避免这种情况)。
如果服务在应用程序之后启动,则文件由 UI 应用创建,并且工作正常。是否可以使.NET Windows服务创建一个可以被Windows应用程序覆盖的文件?
这只是关于文件权限。您需要为希望能够运行应用程序的用户添加写入条目。请参阅此 MSDN 文章,了解如何设置它。
关键部分:
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);
// Add the FileSystemAccessRule to the security settings.
fSecurity.AddAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);