How to convert string into version in .net 3.5?

Error processing SSI file

Answers

  1. Hamza

    • 2021/5/21

    You could always try to decompile the .NET 4.0 Version class - you might be lucky, that it just works in .NET 3.5.

    Otherwise you should look into string split or regular expressions.

  2. Pellegrini

    • 2020/11/11

    Forgive me if im missing something but can't you use the version object constructor passing your version string:.

  3. Hall

    • 2017/1/12

    I want to compare the software version created in 3.5 with the older one. If I try to compare version in 4.0 then it's easy by using Version.Parse but in earlier version this facility is not there.

  4. Hank

    • 2017/6/16

    The string.Join overload accepting IEnumerable<T> was not added until .NET 4. It is not available in .Net 3.5. The classic view in MSDN 

  5. Keegan

    • 2015/3/15

    Forgive me if im missing something but can't you use the version object constructor passing your version string:

    http://msdn.microsoft.com/en-us/library/y0hf9t2e%28v=vs.90%29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

    string str = "0.1.2.3";
    Version v = new Version(str);
    

    This is present in the following .NET Frameworks:

    4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0, 1.1

  6. Riva

    • 2016/3/28

    ' Error: Number out of range in '1.3.5.2150000000'. Remarks. The input parameter must have the following format: major.minor[.build[.revision]] where major, minor, build, and revision are the string representations of the version number's four components: major version number, minor version number, build number, and revision number

  7. Braxton

    • 2017/6/17

    Guid originalGuid = Guid.NewGuid(); // Create an array of string representations of the GUID. string[] stringGuids = { originalGuid.

  8. Callen

    • 2017/7/3

    Type conversion creates a value in a new type that is equivalent to the value of an old type, but does not necessarily preserve the identity (or exact value) of the original object. .NET automatically supports the following conversions: Conversion from a derived class to a base class. This means, for example, that an instance of any class or

  9. Zachariah

    • 2016/10/17

    I ran into a similar problem - I had to parse and sort build numbers so they could be displayed to the user in descending order. I ended up writing my own class to wrap the parts of a build number, and implemented IComparable for sorting. Also ended up overloading the greater than and less than operators, as well as the Equals method. I think it has most of the functionality of the Version class, except for MajorRevision and MinorRevision, which I never used.

    In fact, you could probably rename it to 'Version' and use it exactly like you've done with the 'real' class.

    Here's the code:

    public class BuildNumber : IComparable
    {
        public int Major { get; private set; }
        public int Minor { get; private set; }
        public int Build { get; private set; }
        public int Revision { get; private set; }
    
        private BuildNumber() { }
    
        public static bool TryParse(string input, out BuildNumber buildNumber)
        {
            try
            {
                buildNumber = Parse(input);
                return true;
            }
            catch
            {
                buildNumber = null;
                return false;
            }
        }
    
        /// <summary>
        /// Parses a build number string into a BuildNumber class
        /// </summary>
        /// <param name="buildNumber">The build number string to parse</param>
        /// <returns>A new BuildNumber class set from the buildNumber string</returns>
        /// <exception cref="ArgumentException">Thrown if there are less than 2 or 
        /// more than 4 version parts to the build number</exception>
        /// <exception cref="FormatException">Thrown if string cannot be parsed 
        /// to a series of integers</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if any version 
        /// integer is less than zero</exception>
        public static BuildNumber Parse(string buildNumber)
        {
            if (buildNumber == null) throw new ArgumentNullException("buildNumber");
    
            var versions = buildNumber
                .Split(new[] {'.'},
                       StringSplitOptions.RemoveEmptyEntries)
                .Select(v => v.Trim())
                .ToList();
    
            if (versions.Count < 2)
            {
                throw new ArgumentException("BuildNumber string was too short");
            }
    
            if (versions.Count > 4)
            {
                throw new ArgumentException("BuildNumber string was too long");
            }
    
            return new BuildNumber
                {
                    Major = ParseVersion(versions[0]),
                    Minor = ParseVersion(versions[1]),
                    Build = versions.Count > 2 ? ParseVersion(versions[2]) : -1,
                    Revision = versions.Count > 3 ? ParseVersion(versions[3]) : -1
                };
        }
    
        private static int ParseVersion(string input)
        {
            int version;
    
            if (!int.TryParse(input, out version))
            {
                throw new FormatException(
                    "buildNumber string was not in a correct format");
            }
    
            if (version < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "buildNumber",
                    "Versions must be greater than or equal to zero");
            }
    
            return version;
        }
    
        public override string ToString()
        {
            return string.Format("{0}.{1}{2}{3}", Major, Minor, 
                                 Build < 0 ? "" : "." + Build,
                                 Revision < 0 ? "" : "." + Revision);
        }
    
        public int CompareTo(object obj)
        {
            if (obj == null) return 1;
            var buildNumber = obj as BuildNumber;
            if (buildNumber == null) return 1;
            if (ReferenceEquals(this, buildNumber)) return 0;
    
            return (Major == buildNumber.Major)
                       ? (Minor == buildNumber.Minor)
                             ? (Build == buildNumber.Build)
                                   ? Revision.CompareTo(buildNumber.Revision)
                                   : Build.CompareTo(buildNumber.Build)
                             : Minor.CompareTo(buildNumber.Minor)
                       : Major.CompareTo(buildNumber.Major);
        }
    
        public static bool operator >(BuildNumber first, BuildNumber second)
        {
            return (first.CompareTo(second) > 0);
        }
    
        public static bool operator <(BuildNumber first, BuildNumber second)
        {
            return (first.CompareTo(second) < 0);
        }
    
        public override bool Equals(object obj)
        {
            return (CompareTo(obj) == 0);
        }
    
        public override int GetHashCode()
        {
            unchecked
            {
                var hash = 17;
                hash = hash * 23 + Major.GetHashCode();
                hash = hash * 23 + Minor.GetHashCode();
                hash = hash * 23 + Build.GetHashCode();
                hash = hash * 23 + Revision.GetHashCode();
                return hash;
            }
        }
    }
    
  10. Memphis

    • 2021/4/21

    I already convert a dataset into jason string. How can I convert this Json string into Json object? an ajax call is waiting for this json object 

  11. Palmieri

    • 2020/11/27

    I would like to write the following as a string in my favourite text editor. (Person.Age > 3 AND Person.Weight > 50) OR Person.Age < 3 I would like to take this string and my object instance and evaluate a TRUE or FALSE - i.e. evaluating a Func<Person, bool> on the object instance. Here are my current thoughts:

  12. Ezra

    • 2020/4/29

    This date applies only when running on Windows 10 version 1809, Windows Server 2019 or later. On older versions of Windows, .NET Framework 3.5 Service Pack 

  13. Angelo

    • 2016/8/18

    It seems like you're asking about how to get the versions of any local .NET installations. MSDN has an article about this: http://msdn.microsoft.com/en-us/library/hh925568%28v=vs.110%29.aspx.

    They include the following function therein:

    private static void GetVersionFromRegistry()
    {
         // Opens the registry key for the .NET Framework entry. 
            using (RegistryKey ndpKey = 
                RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
                OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            {
                // As an alternative, if you know the computers you will query are running .NET Framework 4.5  
                // or later, you can use: 
                // using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,  
                // RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            foreach (string versionKeyName in ndpKey.GetSubKeyNames())
            {
                if (versionKeyName.StartsWith("v"))
                {
    
                    RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                    string name = (string)versionKey.GetValue("Version", "");
                    string sp = versionKey.GetValue("SP", "").ToString();
                    string install = versionKey.GetValue("Install", "").ToString();
                    if (install == "") //no install info, must be later.
                        Console.WriteLine(versionKeyName + "  " + name);
                    else
                    {
                        if (sp != "" && install == "1")
                        {
                             Console.WriteLine(versionKeyName + "  " + name + "  SP" + sp);
                        }
                    }
                    if (name != "")
                    {
                        continue;
                    }
                    foreach (string subKeyName in versionKey.GetSubKeyNames())
                    {
                        RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                        name = (string)subKey.GetValue("Version", "");
                        if (name != "")
                            sp = subKey.GetValue("SP", "").ToString();
                        install = subKey.GetValue("Install", "").ToString();
                        if (install == "") //no install info, must be later.
                            Console.WriteLine(versionKeyName + "  " + name);
                        else
                        {
                            if (sp != "" && install == "1")
                            {
                                Console.WriteLine("  " + subKeyName + "  " + name + "  SP" + sp);
                            }
                            else if (install == "1")
                            {
                                Console.WriteLine("  " + subKeyName + "  " + name);
                            }
    
                        }
    
                    }
    
                }
            }
        }
    }
    
  14. John

    • 2018/7/13

    In ASP.NET Core 3.1 and earlier, DateTime values were model-bound as local time, where the timezone was determined by the server. DateTime values bound from input formatting (JSON) and DateTimeOffset values were bound as UTC timezones. In ASP.NET Core 5.0 and later, model binding consistently binds DateTime values with the UTC timezone.

  15. Chase

    • 2019/3/21

    The Parse() method allows conversion of the numeric string into different formats into an integer using the NumberStyles enum e.g string 

  16. Aayan

    • 2016/5/14

    I wanted to remove the dependency on System.Web so that I could parse the query string of a ClickOnce deployment, while having the prerequisites limited to the "Client-only Framework Subset".

  17. Hunter

    • 2017/1/5

    Net version to 3.5. What would be the best way to parse a SQL Server UniqueIdentifier into a System. GlobalIdentifier = new Guid(Convert.

  18. Tate

    • 2015/12/8

    Then you need to change the target version of your application from .NET Framework 3.5 to 4.5 under Project->Properties in Visual Studio 2012 or later and recompile your application. >>Is there any easy way to convert .NET 3.5 supported app. to a .NET 4.5 supported? Or vice versa.

  19. Lorenzo

    • 2018/5/22

    In this series, I try to provide updated answers for common .NET/C# questions. I found that when googling common terms like "convert string to 

  20. Dupont

    • 2020/8/17

    ToSingle ( str1 ) method converts the string type into a Single type . Using the Convert.ToInt ( sngll ) method , the Single type is converted to the 

Comments are closed.

More Posts