I'm writing a service using C# that is supposed to run on a Windows platform as the "local system". I can store small amounts of data in the registry, but if I want to store more data in a file, where do I place such file in? And also how to protect that data from a modification by users with lower access rights?
一般方法是使用ProtectedData类对数据进行加密,然后将它们存储在应用程序可以使用的磁盘上的任何位置(例如,在 LocalApplicationData 特殊文件夹的子文件夹中,您可以使用Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData
call) 获取其位置.
Update to previous answer that may save some troubleshooting time to people using this solution (as I don't have enough reputation to comment):
For Local System Windows service account, Environment.SpecialFolder.LocalApplicationData
may also be resolved to C:\\Windows\\SysWOW64\\config\\systemprofile\\AppData\\Local
instead of C:\\Windows\\System32\\Config\\SystemProfile\\AppData\\Local
.
Using the already mentioned CommonApplicationData instead of LocalApplicationData does not bring this issue.
Information source: https://www.jamescrowley.net/2014/02/24/appdata-location-when-running-under-system-user-account/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.