小菜鸟一步步打造图书馆外挂之十五:自动启动入口的实现

     创建一个Windows应用程序项目AutoStart,添加一个类AutoStartService,用它来提供是否启动程序的服务,它首先去读取用户的设置信息,是每次开机都启动还是每天启动一次,要是每天启动一次就去读取上次启动时保存的时间与今天的时间进行比较,如果不相等就启动,还要重新写入本次的启动时间,实现如下:
using System;
using System.Collections.Generic;
using System.Text;

using LibraryHelper.DALService;

namespace LibraryHelper.AutoStart
{
    
public class AutoStartService
    
{
        
//根据用户的设置(每天启动一次还是每次开机都启动)来决定是否启动应用程序
        public static Boolean IsAutoStart()
        
{
            Boolean start 
= true;   //返回值,为真程序就启动

            
            
//获取启动类型
            SettingXMLService settingXMLService = new SettingXMLService();

            String timeNow 
= DateTime.Now.ToShortDateString().ToString();
            
//上次启动时间
            String timeLastLogin = settingXMLService.GetLastStartTime();

            
//0代表每次开机都启动,1代表每天只启动一次
            String startType = settingXMLService.GetStartType();
            
if (startType == "1")
            
{
                
//如果两个时间相等,说明今天已经登录过
                if (timeNow == timeLastLogin)
                
{
                    start 
= false;
                }

            }


            
//如果两下时间不相等就把今天的时间重写进去
            if (timeNow != timeLastLogin)
            
{
                settingXMLService.SetLastStartTime(timeNow);
            }


            
return start;

        }

    }

}

 

     在Main函数里,调用上面的方法来确定是否启动程序,之后就产生一个工作的对象,在产生工作对象时传给工厂的参数是"AUTO",该工作对象有返回值来决定是不让Application Run起来。

     Main方法如下:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

using LibraryHelper.IDoWorkFactory;
using LibraryHelper.IDoWork;

namespace LibraryHelper.AutoStart
{
    
static class Program
    
{
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main()
        
{
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(
false);

            
//根据用户设置是否启动应用程序
            if (AutoStartService.IsAutoStart())
            
{
                ILibraryHelperDoWork doWork 
= GetDoWorkFactory.CreateDowork("AUTO");

                Boolean isRun 
= doWork.DoWork();

                
if (isRun)
                
{
                    Application.Run();
                }

            }

        }

    }

}

 

 

 

posted @ 2009-03-17 22:56  Done  阅读(642)  评论(0编辑  收藏  举报