实例如下:(这是一段升级配置文件)
LocalUpdate.ini
<?xml version="1.0" encoding="utf-8"?>
<LocalConfig>
<SoftVersion>1.0.0.0</SoftVersion>
<UpdatePublishTime>1.0.0.0</UpdatePublishTime>
<ServerUrl>http://zorro/UpdateContent/</ServerUrl>
<UpdateHistoryList>
<LocalFile num="2009.3.20" lastver="这是最原始的Bata版本" />
</UpdateHistoryList>
</Config>
使用类似结构体的类LocalConfig.cs,代码如下:
/// <summary>
/// 读取主程序的升级配置文件(*.ini)
/// </summary>
public class LocalConfig
{
private string serverUrl = "";
private string softVersion = string.Empty;
// private UpdateHistoryList updateHistoryList = new UpdateHistoryList();
// private string updateIntroduction = "";
private string publishTime = "";
/// <summary>
/// 升级日期
/// </summary>
public string UpdatepublishTime { get { return publishTime; } set { publishTime = value; } }
/// <summary>
/// 升级网址URL
/// </summary>
public string ServerUrl { get { return serverUrl; } set { serverUrl = value; } }
/// <summary>
/// 软件版本号
/// </summary>
public string SoftVersion { get { return softVersion; } set { softVersion = value; } }
/// <summary>
/// 更新列表内容
/// </summary>
//public UpdateHistoryList UpdateHistory
//{
// get { return updateHistoryList; }
// set { updateHistoryList = value; }
//}
public static LocalConfig LoadConfig(string file)
{
XmlSerializer xs = new XmlSerializer(typeof(LocalConfig));
StreamReader sr = new StreamReader(file);
LocalConfig config = xs.Deserialize(sr) as LocalConfig;
sr.Close();
return config;
}
public void SaveConfig(string file)
{
XmlSerializer xs = new XmlSerializer(typeof(LocalConfig));
StreamWriter sw = new StreamWriter(file);
xs.Serialize(sw, this);
sw.Close();
}
}
辅助类文件
/// <summary>
/// 升级文件说明
/// </summary>
public class LocalFile
{
private string num = "";
private string content = "";
[XmlAttribute("num")]
public string Num { get { return num; } set { num = value; } }
[XmlAttribute("content")]
public string UpdateContent { get { return content; } set { content = value; } }
public LocalFile(string num, string updateContent)
{
this.num = num;
this.content = updateContent;
}
public LocalFile()
{
}
}
操作XML的调用代码:
//判断本地升级文件是否存在
strLocalUpdateFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LocalUpdate.ini");
if (!File.Exists(strLocalUpdateFile))
{
MessageBox.Show("本地升级配置文件损坏,无法升级!");
this.Close();
return;
}
//读取本地配置
localUpdateConfig = LocalConfig.LoadConfig(strLocalUpdateFile);
////XML加密
//xmlEncryptAndDecrypt.XmlEncrypt("LocalConfig");
//本地版本
this.lblCurVer.Text = localUpdateConfig.SoftVersion;
//更新内容说明列表(暂时不实现)
// List<UpdateHistoryList> historyList = new List<UpdateHistoryList>();
//升级文件所在的文件夹
string strRemoteDir = localUpdateConfig.ServerUrl;
//获得服务器地址
string serverUrl = GetServerUrl(strRemoteDir);
if (serverUrl != "")
{
bCanUpdate = IsConnectServer(serverUrl);
}

浙公网安备 33010602011771号