菜鸟求c#问题。想要做一个清除系统垃圾的程序。求高人指点~
发布网友
发布时间:2024-09-25 16:49
我来回答
共5个回答
热心网友
时间:2024-09-30 11:07
//代码很简单,绝对能用,全部复制就可以了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.IO;
namespace RubishClear
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region "全局的私有变量"
//设置需要清理的文件夹共5个;
public string[] _RubishPath=new string [5];
//设置用户名.;
string _UserName="";
//设置筛选分隔符;
char[] _split = new char[1] { '\\' };
//设置总文件大小的变量
long _AllMemory = 0;
#endregion
//获取文件夹的路径
void getRubishPath()
{
//获取当前的用户名.方法很多的.//
Microsoft.VisualBasic.Devices.ServerComputer sc = new Microsoft.VisualBasic.Devices.ServerComputer();
string[] All = sc.FileSystem.SpecialDirectories.MyDocuments.Split(_split);
_UserName = All[2];
_RubishPath.SetValue(@"C:\WINDOWS\SoftwareDistribution\Download\", 0);
_RubishPath.SetValue(@"C:\WINDOWS\$hf_mig$\", 2);
_RubishPath.SetValue(sc.FileSystem.SpecialDirectories.Temp + @"\", 3);
// MessageBox.Show(sc.FileSystem.SpecialDirectories.Temp);
_RubishPath.SetValue(@"C:\Documents and Settings\" + _UserName + @"\Recent\", 1);
_RubishPath.SetValue(@"C:\WINDOWS\ie7updates\", 4);
}
//获取垃圾文件的名字&获取要删除的文件的大小./
void getFilename()
{
try
{
foreach (string path in _RubishPath)
{
// string[] arr = Microsoft.VisualBasic.FileIO.FileSystem.GetDirectories(path);
foreach
(string _FileName in Microsoft.VisualBasic.FileIO.FileSystem.GetFiles(path,Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories))
{
try
{
FileInfo fi = new FileInfo(_FileName);//获取文件的所有信息.
_AllMemory += fi.Length;//删除的文件的大小叠加起来.
Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(_FileName);//删除文件.本人原来使用的是VB所以vb的东西搬了过来.
//fi.Delete();//也可以使用fi.edlete事件.
this.listBox1.Items.Add(_FileName);//添加删除的文件.
}
catch (Exception exp)
{
this.listBox2.Items.Add(exp.Message);//添加没有删除的文件.
continue;//如果错误继续执行.
}
}
}
}
catch
{ }
}
//获取&删除垃圾文件夹/
void getDic()
{
try
{
foreach (string path in _RubishPath)
{
foreach
(string _PName in Microsoft.VisualBasic.FileIO.FileSystem.GetDirectories(path, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories))
{
try
{
Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory
(_PName,Microsoft.VisualBasic.FileIO.DeleteDirectoryOption.DeleteAllContents);//删除文件.本人原来使用的是VB所以vb的东西搬了过来.
this.listBox1.Items.Add(_PName);//添加删除文件夹.
}
catch (Exception exp)
{
this.listBox2.Items.Add(exp.Message);//添加没有删除的文件夹.
continue;//如果错误继续执行.
}
}
}
}
catch { }
}
//登录事件
private void Form1_Load(object sender, EventArgs e)
{
getRubishPath();
}
//chilk事件
private void button1_Click(object sender, EventArgs e)
{
getFilename();//开始清空文件.
System.Threading.Thread.Sleep(100);//线程睡眠100毫秒.
getDic();//开始清空文件夹.
la1.Text = "程序共清空了:" + (int)(_AllMemory / 1024) + "K的垃圾文件";
MessageBox .Show ("已经成功删除了"+this.listBox1.Items.Count.ToString()+"项垃圾文件!");
}
}
}
热心网友
时间:2024-09-30 11:08
这类任务用批处理就搞定了……
@echo off
echo 正在清除系统垃圾文件,请稍等......
del /f /s /q %systemdrive%\*.tmp
del /f /s /q %systemdrive%\*._mp
del /f /s /q %systemdrive%\*.log
del /f /s /q %systemdrive%\*.gid
del /f /s /q %systemdrive%\*.chk
del /f /s /q %systemdrive%\*.old
del /f /s /q %systemdrive%\recycled\*.*
del /f /s /q %windir%\*.bak
del /f /s /q %windir%\prefetch\*.*
rd /s /q %windir%\temp & md %windir%\temp
del /f /q %userprofile%\cookies\*.*
del /f /q %userprofile%\recent\*.*
del /f /s /q "%userprofile%\Local Settings\Temporary Internet Files\*.*"
del /f /s /q "%userprofile%\Local Settings\Temp\*.*"
del /f /s /q "%userprofile%\recent\*.*"
echo complete & successful
echo. & pause
把@echo off以下(包括@echo off)的内容存为.bat结尾的文件,双击运行即可。
热心网友
时间:2024-09-30 11:08
最简单的方法是直接用c#调用那段批处理的代码
把那段批处理的代码保存为D:\a.bat
然后这样调用
System.Diagnostics.Process.Start("D:\\b.bat");
热心网友
时间:2024-09-30 11:09
一楼的就很经典 我也在用 优化大师都用不上了
热心网友
时间:2024-09-30 11:10
当然用IO包了