How do I set the TEMP environment variable for the "Network Service" user?
We have a system that uses Path.GetTempFile and Path.GetTempPath calls to work with temporary files fairly frequently. This system also runs as the "Network Service" user. We're finding that we're running out of room on the C drive (for other issues, our temp files are cleaned up correctly) and would like to be able to move the temp directory to a different drive.
The easiest solution to this seems to be to change the TMP or TEMP environment variables for the Network Service user, but I only seem to be able to set my own user or the "system" variables that are overwritten by the Network Service user profile. How do I set these variables for the Network Service user?
Solution 1:
It's actually pretty difficult. You can use the setx
command to set persistent environment variables for other users, but this won't work for NetworkService, because the service accounts have no passwords (so your credentials will always be rejected).
The only way that I know of to do this is using the task scheduler:
- Set up a task to run as the user "network service".
- Leave the triggers empty.
- For the action, have it run
cmd.exe
with the parameters/c setx temp [target]
. - Run the task manually.
- Delete the task.
This will work to set a persistent environment variable for the NetworkService user. As to whether this will successfully work to relocate the temporary files for that user, or whether it will cause any unpredictable side-effects, I can't say for certain. You may need to reboot to verify if it's putting the temp files where you want them.
You can verify that the environment variable has been changed for the correct user by using the environment
command within WMIC.
Solution 2:
Environment variables for Network Service
account are stored in Windows Registry and can be modified by regedit: HKEY_USERS\S-1-5-20\Environment
.