Update@2005.8.19
TimeDog最新版本 v13
下载地址
http://www2.zzu.edu.cn/xb/timedog.exe
源代码这里下载
http://www2.zzu.edu.cn/xb/timedog_src.rar
==============
本人以前做过不少的ASP.NET的web应用程序开发,但是,在桌面应用程序开发方面不熟悉,基本没有开发过这方面的软件,如今,本人想通过共同开发来交流,利用业余时间使自己具有开发应用软件的经验,希望C#初学者,或者已经掌握C# Windows应用程序开发的.net爱好者与我交流.QQ:154222225.
项目初步需求如下:
名称:TimeDog
实现功能:能够累计计算电脑从某时刻起,其运行的总体时间.软件随操作系统启动,并开始计时,用户关闭操作系统时,存储运行时间,以便和下次的运行时间进行累加.操作系统运行时期内,不允许用户结束本软件进程.
共同开发人员:
#caca(卡卡) http://www.cnblogs.com/caca
#frogman http://www.cnblogs.com/frogman/
开发进度如下:
1,在CSDN发表的求助:http://community.csdn.net/Expert/topic/3797/3797941.xml?temp=.8971979
2,自启动解决方案:
使程序开机自动运行(修改注册表?):
将你的程序加入“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run”
//自动运行
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey run = hklm.OpenSubKey ( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" , true ) ;
if(run.GetValue( "LookOut") == null)
{
string eFileName=Application.ExecutablePath + " ";//含可执行文件名
run.SetValue ( "LookOut" , exeFileName );
}
run.Close();
hklm.Close();
RegistryKey hklm = Registry.LocalMachine ;
RegistryKey run = hklm.OpenSubKey ( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" , true ) ;
if(run.GetValue( "LookOut") == null)
{
string eFileName=Application.ExecutablePath + " ";//含可执行文件名
run.SetValue ( "LookOut" , exeFileName );
}
run.Close();
hklm.Close();
3,关于.NET的Hooks
http://www.codeproject.com/csharp/GlobalSystemHook.asp
4,自启动以及计时功能已经初步实现,考虑到微量数据的存储,暂时直接存储到注册表内部.
private void Form1_Load(object sender, System.EventArgs e)
{
//检测注册表项是否已经设置了自动启动
RegistryKey LM=Registry.LocalMachine;
RegistryKey run=LM.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
if(run.GetValue("TimeDog")==null)
{
string exeFileName=Application.StartupPath+@"\TimeDog.exe";
run.SetValue("TimeDog",exeFileName);
}
//设置注册表项,用来存储系统运行时间
if(run.GetValue("TimeDogRunTime")==null)
{
run.SetValue("TimeDogRunTime","00:00:00");
}
//设置开始时间
run.SetValue("TimeDogStartTime",DateTime.Now.ToString());
//系统运行时间
MessageBox.Show("系统运行了:"+run.GetValue("TimeDogRunTime").ToString());
run.Close();
LM.Close();
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//用户关闭窗口时候,计算程序运行时间,并存入注册表
RegistryKey LM=Registry.LocalMachine;
RegistryKey run=LM.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
TimeSpan LastAll=(TimeSpan)(Convert.ToDateTime(run.GetValue("TimeDogRunTime")).TimeOfDay);//获得上次总体运行时间
DateTime TimeDogStartTime=Convert.ToDateTime(run.GetValue("TimeDogStartTime").ToString());//获得本次开始运行时间
TimeSpan ts=DateTime.Now-TimeDogStartTime;//本次运行时间
TimeSpan AllTime=LastAll+ts;//本次在内的总体时间
if(run.GetValue("TimeDogRunTime")==null)
{
run.SetValue("TimeDogRunTime","0");
}
else
run.SetValue("TimeDogRunTime",AllTime.ToString());
}
{
//检测注册表项是否已经设置了自动启动
RegistryKey LM=Registry.LocalMachine;
RegistryKey run=LM.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
if(run.GetValue("TimeDog")==null)
{
string exeFileName=Application.StartupPath+@"\TimeDog.exe";
run.SetValue("TimeDog",exeFileName);
}
//设置注册表项,用来存储系统运行时间
if(run.GetValue("TimeDogRunTime")==null)
{
run.SetValue("TimeDogRunTime","00:00:00");
}
//设置开始时间
run.SetValue("TimeDogStartTime",DateTime.Now.ToString());
//系统运行时间
MessageBox.Show("系统运行了:"+run.GetValue("TimeDogRunTime").ToString());
run.Close();
LM.Close();
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//用户关闭窗口时候,计算程序运行时间,并存入注册表
RegistryKey LM=Registry.LocalMachine;
RegistryKey run=LM.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
TimeSpan LastAll=(TimeSpan)(Convert.ToDateTime(run.GetValue("TimeDogRunTime")).TimeOfDay);//获得上次总体运行时间
DateTime TimeDogStartTime=Convert.ToDateTime(run.GetValue("TimeDogStartTime").ToString());//获得本次开始运行时间
TimeSpan ts=DateTime.Now-TimeDogStartTime;//本次运行时间
TimeSpan AllTime=LastAll+ts;//本次在内的总体时间
if(run.GetValue("TimeDogRunTime")==null)
{
run.SetValue("TimeDogRunTime","0");
}
else
run.SetValue("TimeDogRunTime",AllTime.ToString());
}
5,实现类似QQ最小化形式的功能,参考了:
C#编写最小化时隐藏为任务栏图标的Window appllication.
6,禁止用户点击关闭按钮时关闭应用程序.
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancle=true;
}
{
e.Cancle=true;
}
7,项目源文件提供下载
8,关于Windows服务程序的设计
用Visual C#创建Windows服务程序
用C#创建Windows服务(Windows Services)
9,能创建一个"不死进程"的程序将更好,但是,CSDN的网友说了,无法实现进制用户结束进程的,于是有人提议用进程注入,这些可能只能用汇编或者C实现了,C#应该不能实现,现在做出了Windows服务程序版本,22K的程序,而且知识在服务启动,停止的时候作些文件读取操作,但是其占用的内存(7.35M)着实不太让我满意,程序的优化又从何处着手呢?
Windows服务版本提供源代码下载 (程序的安装参考上面的两篇文章)
10,Windows服务版本本来是禁止用户停止服务的,在用户关闭电脑的时候触发一个事件并记录时间,所以应该重写OnShutdown()函数,而不是OnStop()函数
protected override void OnShutdown()
{
base.OnShutdown ();
WriteLog("end@"+DateTime.Now.ToString());
WriteLog("===========");
//记录运行时间
WriteTime();
}
{
base.OnShutdown ();
WriteLog("end@"+DateTime.Now.ToString());
WriteLog("===========");
//记录运行时间
WriteTime();
}
11,放两个程序的图片
(下图:程序以后台服务的形式运行,禁止停止服务)
(下图:程序的进程图,20K的程序,占用内存怎么那么多呢???C#写的程序“很吃内存”,有没有优化的方法呢?是不是因为后台服务程序调用了很多.net的程序集,虽然代码就几行--Microsoft简化的程序的设计难度。)
(下图:程序运行一段时间后,占用内存大幅度降低)
(下图:TimeDogSee,用来查看系统共运行的时间)
12,下面的任务就是做成分布式的应用程序,我们一户为了计时收费,全部安装了本软件,于是想通过网络编程,是用户可以发送运行的时间以及详细记录,服务器也可以通过网络进行对客户端的运行时间进行控制-比如:清零。
13,关于c#方面的网络编程的书籍,记得上个学期的暑假里面看过一本书:
C# Network Programming | |
ISBN:0782141765 | |
Sybex © 2003 (647 pages) |
里面涉及到一个网络抓包工具:WinPcap(Ethereal也是调用他实现网络抓包的),可以在这里下载:
http://netgroup-serv.polito.it/netgroup/tools.html
14,软件运行的很是成功,不过准备改进一下:
(1)加入定时计算结果,防止以外调电,或者用户或者系统关闭进程(有记录).
(2)存储的数据进行加密,路径更为隐蔽.
(3)对软件进行部署,方便安装,卸载与更新.