我已经在版本中使用了“*”来简化控制版本,并为客户提供信息。我有一个在手持设备上运行的小程序(PointOfSell),有时会发生更新。:-) 在我的代码中,我使用:
来自 AssemblyInfo.cs 的片段
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.*")]
// Need to comment this line,
//[assembly: AssemblyFileVersion("1.0.0.0")]
//to avoid the following error:
// ...\Properties\AssemblyInfo.cs(36,32,36,39):
// warning CS7035: The specified version string does not conform to
// the recommended format - major.minor.build.revision
// warning AL1053: The version '1.0.*' specified for
// the 'file version' is not in the normal 'major.minor.build.revision' format
检查更新时,我从一个小型 Web 服务向客户端发送一个版本列表。
void smsReceive1_OnCabUpdate(object sender, CabDataEventArgs e)
{
try
{
var cabVer = e.CabVersion;
var sms1 = (SmsService.SmsService)sender;
SmsService.CabinetVersion progVer = null;
var exeApp = Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
foreach (var item in cabVer)
{
if (string.Compare(item.FileName, exeApp, true) == 0)
{
progVer = item;
break;
}
}
var msgForm = new MessageBoxForm();
if (!forceUpdate && !WillUpdateVersion(progVer))
{
Buzzer.Warning();
sms1.StopReceive();
msgForm.Message = "\r\nNewest Version!\r\n" +
"Version\r\n" + progVer.AssemblyVersion.ToString() + "\r\n" +
"Last Modified:\r\n" + progVer.AssemblyVersion.ToDateTime().ToString("yy/MM/dd HH:mm") + "\r\n" +
"\r\nNo need to update";
msgForm.OKShowDialog();
return;
}
//
byte[] buffer = e.CabData;
var filename = "\\ramdisk\\Setup.cab";
if (File.Exists(filename))
{
File.Move(filename, string.Format("{0:yyMMdd_HHmmss}_Setup.cab"));
}
var fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();
//
Buzzer.Warning();
//Stop SmsReceive
sms1.StopReceive();
msgForm.Message = "There is an update.\r\n" +
"Version \r\n" + progVer.AssemblyVersion.ToString() + "\r\n" +
"Last Modified:\r\n" + progVer.AssemblyVersion.ToDateTime().ToString("yy/MM/dd HH:mm") + "\r\n" +
"※After the update\r\nIt will restart automatically";
msgForm.SetSubMessage("Do you want to update?");
var resp = msgForm.OKCancelShowDialog(true) == DialogResult.OK;
if (resp)
{
CabFileUpdatePath = filename;
UpdateApplication = true;
Invoke((Action)(() => restartApplicationTimer.Enabled = true));
}
else
{
File.Delete(filename);
}
}
finally
{
if (smsReceive1 != null) smsReceive1.ServicePaused = ServiceStatus.None;
}
}
private bool WillUpdateVersion(SmartShooter.SmsService.CabinetVersion ver)
{
var appVer = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
if (ver.CompareToExe(appVer) > 0) return true;
return false;
}
progVer.AssemblyVersion.ToDateTime().ToString("yy/MM/dd HH:mm")
最后,这是因为我在版本中使用了'*'。
public partial class MyVersion {
public ushort Build { get; set; }
public ushort Major { get; set; }
public ushort Minor { get; set; }
public ushort Revision { get; set; }
public override string ToString()
{
return string.Format("{0}.{1}.{2}.{3}", Major, Minor, Build, Revision);
}
public DateTime ToDateTime()
{
var dt = new DateTime(2000, 1, 1, 0, 0, 0);
//plus days
dt = dt.AddDays(Build);
//plus seconds
dt = dt.AddSeconds(Revision * 2);
return dt;
}
}
我使用“Build”值来查找项目编译的日期,并使用“Revision”来获取精确的时间。