.Net(c#)通用驗證版本新舊的方法
版本號碼常識:
// 組件的版本資訊是由下列四項值構成:
//
// 主要版本(第一碼)
// 次要版本(第二碼)
// 組建編號(第三碼)
// 修訂編號(第四碼)
//
[assembly: AssemblyVersion("1.1.0.2")]
[assembly: AssemblyFileVersion("1.1.0.2")]
驗證版本
public bool CheckCurrentVersion(string CurrentVersion, string ServerVersion)
{
int i;
Exception ex;
string[] ListCurrentVersion = CurrentVersion.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);//返回字符串數組
string[] ListServerVersion = ServerVersion.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);//同上
bool IsUpdate = false;
int iCurrent = 0;
int iServer = 0;
if (ListCurrentVersion.Length == ListServerVersion.Length) //Godwar:相同版本長度的判斷
{
for (i = 0; i < ListCurrentVersion.Length; i++)
{
try
{
iServer = int.Parse(ListServerVersion[i]);
}
catch (Exception exception1)
{
ex = exception1;
}
try
{
iCurrent = int.Parse(ListCurrentVersion[i]);
}
catch (Exception exception2)
{
ex = exception2;
}
if (iServer < iCurrent)
{
return IsUpdate;
}
if (iServer > iCurrent)
{
return true;
}
}
return IsUpdate;
}
bool IsListCurrentVersionMax = false;
if (ListCurrentVersion.Length > ListServerVersion.Length) //Godwar:當版本長度不一致,按照從左向右依主次順序判斷
{
IsListCurrentVersionMax = true;
}
int iMin = (ListCurrentVersion.Length < ListServerVersion.Length) ? ListCurrentVersion.Length : ListServerVersion.Length;
for (i = 0; i < iMin; i++)
{
try
{
iServer = int.Parse(ListServerVersion[i]);
}
catch (Exception exception3)
{
ex = exception3;
}
try
{
iCurrent = int.Parse(ListCurrentVersion[i]);
}
catch (Exception exception4)
{
ex = exception4;
}
if (iServer < iCurrent)
{
return IsUpdate;
}
if (iServer > iCurrent)
{
return true;
}
}
return IsUpdate; // 返回是否需要升級,即本地版本是否小於服務器最新版本
}
public bool CheckCurrentVersion(string CurrentVersion, string ServerVersion)
{
int i;
Exception ex;
string[] ListCurrentVersion = CurrentVersion.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);//返回字符串數組
string[] ListServerVersion = ServerVersion.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);//同上
bool IsUpdate = false;
int iCurrent = 0;
int iServer = 0;
if (ListCurrentVersion.Length == ListServerVersion.Length) //Godwar:相同版本長度的判斷
{
for (i = 0; i < ListCurrentVersion.Length; i++)
{
try
{
iServer = int.Parse(ListServerVersion[i]);
}
catch (Exception exception1)
{
ex = exception1;
}
try
{
iCurrent = int.Parse(ListCurrentVersion[i]);
}
catch (Exception exception2)
{
ex = exception2;
}
if (iServer < iCurrent)
{
return IsUpdate;
}
if (iServer > iCurrent)
{
return true;
}
}
return IsUpdate;
}
bool IsListCurrentVersionMax = false;
if (ListCurrentVersion.Length > ListServerVersion.Length) //Godwar:當版本長度不一致,按照從左向右依主次順序判斷
{
IsListCurrentVersionMax = true;
}
int iMin = (ListCurrentVersion.Length < ListServerVersion.Length) ? ListCurrentVersion.Length : ListServerVersion.Length;
for (i = 0; i < iMin; i++)
{
try
{
iServer = int.Parse(ListServerVersion[i]);
}
catch (Exception exception3)
{
ex = exception3;
}
try
{
iCurrent = int.Parse(ListCurrentVersion[i]);
}
catch (Exception exception4)
{
ex = exception4;
}
if (iServer < iCurrent)
{
return IsUpdate;
}
if (iServer > iCurrent)
{
return true;
}
}
return IsUpdate; // 返回是否需要升級,即本地版本是否小於服務器最新版本
}