龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

推荐C#开发桌面自动更新程序(服务端与客户端)(4)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
客户端 新建一个WinForm应用程序项目,名为Update: 建好之后直接删掉Form1.cs吧,此程序不需要界面,在Program.cs中写代码就可以了。 同样需要引入GZip类用于

客户端

新建一个WinForm应用程序项目,名为Update:

image

建好之后直接删掉Form1.cs吧,此程序不需要界面,在Program.cs中写代码就可以了。

 

同样需要引入GZip类用于解包:

image

然后编写代码:

[STAThread]
static void Main()
{
    try
    {
        var d = DateTime.Now;
        while (DateTime.Now.Subtract(d).TotalSeconds < 10) Application.DoEvents();
        GZip.解压缩(Path.Combine(Application.StartupPath, "update.data"), Application.StartupPath);
    }
    catch { }
}

这里的作用就是等待10秒,然后解包update.data文件,覆盖到当前目录中。

现在来建立主程序,主程序是WinForm、命令行、WPF都可以,我们新建一个WPF应用程序,命名为MyAPP:

image

为程序添加服务引用:

image

这里的地址使用的是本地的调试地址。

为了检测主程序自身的版本号,还需要添加对System.Windows.Forms的引用。

然后开始设计界面,这里仅为演示更新操作,所以界面上只是简单的设计了更新相关的提示、操作控件:

image

代码为:

<Window x:Class="MyApp.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="377" Loaded="Window_Loaded" Closed="Window_Closed">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Label Margin="0" Name="label1" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Hidden">检测到新版本,是否下载?</Label>
        <Button Grid.Row="1" Height="23" Name="button1" VerticalAlignment="Center" Visibility="Hidden" Click="button1_Click">开始下载</Button>
        <Label Grid.Row="2" Margin="0" Name="label2" VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="Hidden">更新包已下载完毕,在程序关闭后将自动执行更新操作。</Label>
    </Grid>
</Window>

需注意的是,这里控件都被设置为Visibility="Hidden",我们将会在需要时再将其显示出来。

 

精彩图集

赞助商链接