【发布时间】:2017-08-22 00:39:06
【问题描述】:
从 Windows 服务写入文件系统
从控制台应用程序中,我可以使用
System.IO.File.WriteAllText(@"C:\_dev\some_path\Log.txt", "new file additions");
但是在我的新 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?我在WriteAllText() 中指定的文件路径是C:\_dev\some_path\Log.txt?
我确信这与 Idendity 有关,但我是使用 Windows 服务的新手,并且确信我只是忽略了一些简单的事情。
具有访问权限的当前用户: 1. 认证用户 2.系统 3. 管理员(计算机\管理员) 4. 用户(计算机\用户)
我也尝试并添加了这些帐户,但仍然遇到上述相同的错误 5. 本地服务 6. 服务 7.IUSR
我的 Windows 服务如何写入文件以及为什么 WriteAllText() 尝试使用 C:\WINDOWS\system32。
【问题讨论】:
-
您正在运行该进程的用户没有写入 System32 文件夹的权限。更改 UAC 设置并在其他位置写入
-
您是否尝试过打开Services.msc,右键单击服务并单击属性。在弹出的窗口中,单击 Tab 上的登录并尝试将帐户设置为您知道可以访问您描述的文件夹路径的帐户?
标签: c# windows-services file-permissions