none
C# service to auto delete all the clutter on the PC on start. RRS feed

  • Question

  • I'm new to C# so bare with me.

    All I want to do is make a simple service that on PC reboots, the service will simply clear the clutter off the hard drive and set some security permissions on a folder that seems to always lose it's settings.

    I can get most of the code to work flawlessly except the C:\users\<username main account>\AppData\Local\Temp to delete.

    I simply can't find a way to get the path to that folder on test machines.

    I know it's because the service is running under system or network or local service, I tried all but no matter what, path to %temp% is not working.

    Please help me find a simple solution.

    Thursday, May 24, 2018 7:20 PM

All replies

  • I'm new to C# so bare with me.

    All I want to do is make a simple service that on PC reboots, the service will simply clear the clutter off the hard drive and set some security permissions on a folder that seems to always lose it's settings.

    I can get most of the code to work flawlessly except the C:\users\<username main account>\AppData\Local\Temp to delete.

    I simply can't find a way to get the path to that folder on test machines.

    I know it's because the service is running under system or network or local service, I tried all but no matter what, path to %temp% is not working.

    Please help me find a simple solution.


    Is there something wrong with the tools the O/S provides for disk-cleanup as the administrator with rights to do this? 
    Thursday, May 24, 2018 7:45 PM
  • Yes, they are manual and also there are several locations that have log buildups that I simply want to automate on our customers computers.

    Example, mongoDB logs build up too much, the iis logs, windows temp, %temp%, our IIS site temp folder build up to much over time, etc.

    I want to distribute a simple solution to automate cleanup of these common problem folders.

    Thursday, May 24, 2018 7:52 PM
  • A user's temp directory is configurable so you cannot simply assume it'll be there. The only real way to do this is to load the user's profile and get their temp directory.

    If you did want to simply assume it was there then, assuming you have the right permissions, you should be able to access it. But nothing short of an admin would give you those permissions. If you did have the correct permissions then you should be able to browse the directory. If you can browse it then you can delete files most likely. But note that many of the files/folders in the temp area are locked so you won't be able to delete them all. Also note that many products (e.g. Visual Studio) write log files there that, if deleted, would prevent diagnose of installation issues.



    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, May 24, 2018 9:17 PM
    Moderator
  • Are you trying to do this at boot time prior to login, or after login?  Prior to login, there is no "logged in user", so there's no way to get a username.  You might consider iterating through all of the subdirectories in C:\Users, figuring out which ones are system accounts and which are real users. and clear all of those.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Friday, May 25, 2018 6:17 AM