C# Windows服务写入文件-UnauthorizedAccessException

C# Windows服务写入文件-UnauthorizedAccessException,c#,windows-services,file-permissions,C#,Windows Services,File Permissions,从Windows服务写入文件系统 从控制台应用程序中,我可以使用 System.IO.File.writealText(@“C:\\u dev\some\u path\Log.txt”,“新文件添加”) 但是,在我的新Windows服务中使用相同的代码,我无法通过以下原因实现相同的功能: `Service cannot be started. System.Exception: unable to save file! - filePath=C:\_dev\some_path\Log.txt

从Windows服务写入文件系统

从控制台应用程序中,我可以使用
System.IO.File.writealText(@“C:\\u dev\some\u path\Log.txt”,“新文件添加”)

但是,在我的新Windows服务中使用相同的代码,我无法通过以下原因实现相同的功能:

`Service cannot be started. System.Exception: unable to save file! - 
filePath=C:\_dev\some_path\Log.txt 
type=System.UnauthorizedAccessException , 
message=Access to the path 'C:\WINDOWS\system32\filePath' is denied.
innerException=, 
stackTrace=   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
   at Sys...`
为什么要使用文件路径
C:\WINDOWS\system32\filePath
?我在
writealText()
中指定的文件路径是
C:\\u dev\some\u path\Log.txt

我确信这与Idendity有关,但我是一个使用Windows服务的新手,我确信我忽略了一些简单的东西

当前具有访问权限的用户: 1.验证用户组 2.系统 3.管理员(计算机\管理员) 4.用户(计算机\用户)

我也尝试添加了这些帐户,但仍然得到了上面相同的错误 5.当地服务 6.服务 7.IUSR


我的Windows服务如何写入文件?为什么
writealText()
尝试使用
C:\Windows\system32

仍然没有写入磁盘,但通过以下方式启动服务:

  • 单击“登录”选项卡
  • 选择“本地系统帐户” A.选中“允许服务与桌面交互”

  • 仍然没有写入磁盘,但已通过以下方式启动服务:

  • 单击“登录”选项卡
  • 选择“本地系统帐户” A.选中“允许服务与桌面交互”

  • 正在运行进程的用户没有在System32文件夹上写入的权限。更改UAC设置并在其他位置写入您是否尝试打开Services.msc,右键单击服务并单击属性。在弹出的窗口中,单击“登录”选项卡,然后尝试将帐户设置为您知道可以访问所描述的文件夹路径的帐户?您正在运行该进程的用户没有在System32文件夹上写入的权限。更改UAC设置并在其他位置写入您是否尝试打开Services.msc,右键单击服务并单击属性。在弹出的窗口中,单击“登录”选项卡,然后尝试将帐户设置为您知道可以访问所描述的文件夹路径的帐户?