判断程序是自动启动还是用户启动

在设计自动更新程序的时候,希望在应用程序启动的同时,在后台启动更新程序,不弹出更新状态窗口,

而在用户点击应用程序的更新功能的时候,希望在前台启动更新程序,弹出更新状态窗口,所以要在启动时判断更新程序是自动启动还是用户启动。

WPF:假设用户的更新程序是WPF程序,则在App.xaml.cs文件中修改为以下代码:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        new MainWindow(e.Args);
    }
}

然后修改MainWindow的构造函数为MainWindow(string[] args)。

WinForm:假设用户的更新程序是WinForm程序,则在Program.cs文件中修改为以下代码:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1(args));
}

然后修改Form1的构造函数为Form1(string[] args)。

---------------------------------------------------华丽分割线----------------------------------------------------

然后在启动更新程序的程序中用以下代码启动更新程序:

ProcessStartInfo startInfo = new ProcessStartInfo(startFile);
startInfo.Arguments = startArguments;
Process.Start(startInfo);

startFile:更新程序文件所在路径
startArguments:启动参数

如此即可实现,带参数的启动,当更新程序启动的时候对args参数进行判断,即可分别显示。

posted on 2012-10-17 19:10  guyichang  阅读(1184)  评论(1编辑  收藏  举报

导航