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

本文介绍了一个Windows程序的自动启动机制,通过判断用户设置来决定程序是否随系统启动或每日启动一次,并通过比较当前日期与上次启动日期来避免重复启动。
     创建一个Windows应用程序项目AutoStart,添加一个类AutoStartService,用它来提供是否启动程序的服务,它首先去读取用户的设置信息,是每次开机都启动还是每天启动一次,要是每天启动一次就去读取上次启动时保存的时间与今天的时间进行比较,如果不相等就启动,还要重新写入本次的启动时间,实现如下:
using System;
using System.Collections.Generic;
using System.Text;

using LibraryHelper.DALService;

namespace LibraryHelper.AutoStart
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class AutoStartService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//根据用户的设置(每天启动一次还是每次开机都启动)来决定是否启动应用程序
        public static Boolean IsAutoStart()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            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")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//如果两个时间相等,说明今天已经登录过
                if (timeNow == timeLastLogin)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    start 
= false;
                }

            }


            
//如果两下时间不相等就把今天的时间重写进去
            if (timeNow != timeLastLogin)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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
ExpandedBlockStart.gifContractedBlock.gif
{
    
static class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

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

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

                Boolean isRun 
= doWork.DoWork();

                
if (isRun)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    Application.Run();
                }

            }

        }

    }

}

 

 

 

转载于:https://www.cnblogs.com/chenzehe/archive/2009/03/17/1414785.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值