Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello! I am currently working on a software that will delete some junk on my PC, stuff that I normally delete manually but since I can do it automaticlly why not right?


I usually delete everything in my temp folder.. That is if you hit the Windows Key + R and type %temp% and then I delete everything there.. The thing is, I was talking over this with a friend of mine and we agreed on something like this might work


C#
System.IO.DirectoryInfo di = new DirectoryInfo("YourPath");

foreach (FileInfo file in di.GetFiles())
{
    file.Delete(); 
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
    dir.Delete(true); 
}



But the thing is, he wanted me to send him the software once im done..

And lets say I change the YourPath to the exact path on my computer it wont work on his because lets say mine is

C++
C:\Users\Dev\AppData\Local\Temp


He might have a different name on his PC..


So my question is..Is there a way I could bring up maybe the CMD when a button is being pressed or is there a short code for accessing the temp files in the .NET library?

What I have tried:

I've tried looking over some examples on StackOverflow but nothing that applied to my situation.. And yes I am well aware of CCleaner
Posted
Updated 4-May-16 19:55pm
Comments
Ehsan Sajjad 4-May-16 15:29pm    
have you looked at : http://stackoverflow.com/questions/29173681/specifically-getting-the-system-temp-path-in-c-sharp

Sounds like you're looking for the Path.GetTempPath method[^]:
C#
string tempPath = Path.GetTempPath();
DirectoryInfo di = new DirectoryInfo(tempPath);
...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-May-16 15:37pm    
5ed.
I would note: you don't always need DirectoryInfo, it could be System.IO.Directory, or none of the above.
—SA
What worked for me was

C#
string tempPath = Path.GetTempPath();
DirectoryInfo di = new DirectoryInfo(tempPath);

foreach (FileInfo file in di.GetFiles())
{
    file.Delete();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900