분할 기능을 사용하지 않고 버전 번호 비교
버전 번호를 어떻게 비교합니까?
예를 들어 :
x = 1.23.56.1487.5
y = 1.24.55.487.2
버전 클래스를 사용할 수 있습니까?
http://msdn.microsoft.com/en-us/library/system.version.aspx
IComparable 인터페이스가 있습니다. 5 개의 부분 버전이 없습니까? 입력이 가정하면 일반적인 .NET 4 부분 버전을 사용하는 샘플이 있습니다.
static class Program
{
static void Main()
{
string v1 = "1.23.56.1487";
string v2 = "1.24.55.487";
var version1 = new Version(v1);
var version2 = new Version(v2);
var result = version1.CompareTo(version2);
if (result > 0)
Console.WriteLine("version1 is greater");
else if (result < 0)
Console.WriteLine("version2 is greater");
else
Console.WriteLine("versions are equal");
return;
}
}
major.minor.build.revision 구성표를 사용할 수 있으면 .Net 버전 클래스를 사용할 수 있습니다 . 어떤 종류의 파싱을 구현하고 차이가 있는지 두 버전이 동일을 계속해야합니다.
@JohnD의 답변 외에도 Split ( '.') 또는 다른 암호화 <-> int 변환 부풀림을 사용하지 않고 부분 버전 번호 만 비교해야 할 수도 있습니다. 방금 확장 할 수있는 확장 방법 인 CompareTo를 추가 인수 1 개-비교할 버전 번호의 중요한 부분 수 (1과 4 사이).
public static class VersionExtensions
{
public static int CompareTo(this Version version, Version otherVersion, int significantParts)
{
if(version == null)
{
throw new ArgumentNullException("version");
}
if(otherVersion == null)
{
return 1;
}
if(version.Major != otherVersion.Major && significantParts >= 1)
if(version.Major > otherVersion.Major)
return 1;
else
return -1;
if(version.Minor != otherVersion.Minor && significantParts >= 2)
if(version.Minor > otherVersion.Minor)
return 1;
else
return -1;
if(version.Build != otherVersion.Build && significantParts >= 3)
if(version.Build > otherVersion.Build)
return 1;
else
return -1;
if(version.Revision != otherVersion.Revision && significantParts >= 4)
if(version.Revision > otherVersion.Revision)
return 1;
else
return -1;
return 0;
}
}
public int compareVersion(string Version1,string Version2)
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"([\d]+)");
System.Text.RegularExpressions.MatchCollection m1 = regex.Matches(Version1);
System.Text.RegularExpressions.MatchCollection m2 = regex.Matches(Version2);
int min = Math.Min(m1.Count,m2.Count);
for(int i=0; i<min;i++)
{
if(Convert.ToInt32(m1[i].Value)>Convert.ToInt32(m2[i].Value))
{
return 1;
}
if(Convert.ToInt32(m1[i].Value)<Convert.ToInt32(m2[i].Value))
{
return -1;
}
}
return 0;
}
어떤 버전의 버전의 비교 방법을 직접 사용할 수없는 경우 (예 : 클라이언트-서버 시나리오) 다른 방법은 버전에서 긴 숫자를 추출한 다음 숫자를 서로 비교하는 것입니다. 그러나 숫자는 다음과 같은 형식이어야합니다. Major, Minor 및 Revision은 2 자리, Build는 4 자리입니다.
버전 번호를 추출하는 방법 :
var version = Assembly.GetExecutingAssembly().GetName().Version;
long newVersion = version.Major * 1000000000L +
version.Minor * 1000000L +
version.Build * 1000L +
version.Revision;
그리고 다른 곳에서 수 있습니다.
if(newVersion > installedVersion)
{
//update code
}
참고 : installedVersion은 이전에 추출 된 긴 숫자입니다.
참고 URL : https://stackoverflow.com/questions/7568147/compare-version-numbers-without-using-split-function
'IT' 카테고리의 다른 글
asp.net System.Web.WebPages.Razor.Configuration.HostSection에서 작동이 중지. (0) | 2020.07.23 |
---|---|
프로그래밍 방식으로 전화를 거는 방법? (0) | 2020.07.23 |
새로운 Firebase 클라우드 메시징 시스템이 포함 된 알림 아이콘 (0) | 2020.07.23 |
한 달에 일수에서 (0) | 2020.07.23 |
.NET에서 전달은 어떻게 전달됩니까? (0) | 2020.07.23 |