Folder name increment like 5.0.0.x in C #

I have a ~ / ConfigurationDirectory folder. Subfolders in this folder are named as follows

5.0.0.1
5.0.0.2
5.0.0.3
...

Now you need the following: Identify the folder that has the "largest" name, numerically and create a copy of the folder. Rename the new folder as 5.0.0.n + 1 (assuming that the largest folder available for digital files is 5.0.0.n)

I wrote code that will identify the largest named folder. In addition, I wrote code that will make a copy of the folder and subfolders. What I cannot get is how do I get the name of a new folder, i.e. 5.0.0.n + 1

How to do it in C #? Any pointers would be sufficient, not full coding.

Thank!

+3
source share
5 answers

Assuming the numbers you're working with are not simple four-part version numbers, you'll want to use string.Split () to split the folder name, and then Convert.ToInt32 () or int.Parse () to turn the last piece in number. From there you increment it and then use something like string.Format () to get it back to the folder name.

If, however, you really work with simple version numbers, then using the class System.Version(in particular, the methods Parse()or TryParse()and ToString()) will be a much simpler implementation.

+4
source

You can use string.LastIndexOffor this purpose (this is much easier than using string.Split):

static string GetNextFolderName(string folderName)
{
    int lastDotPosition = folderName.LastIndexOf('.');
    string lastPartOfFolderName = folderName.Substring(lastDotPosition + 1);

    int number;
    if (int.TryParse(lastPartOfFolderName, out number))
    {
        number++;
        return folderName.Substring(0, lastDotPosition + 1) + number.ToString();
    }
    else
    {
        // You've got a problem on your hands, here.
        throw new FormatException();
    }
}

. , , , Version.TryParse. :

  • Version.TryParse .NET 4.0. .NET 4.0; ( ) .
  • , . , . , , Version.TryParse , : Version. , , , , , . : 30% , Version.TryParse.
  • , , , - Version.TryParse , - . , , , . Version.TryParse, 200%? ; , , , . .

, , , "" . , , - . , , , .

+3

, imo:

Version version;
if (Version.TryParse("5.0.0.0", out version))
{
    // your logic here
    return new Version(
         version.Major,
         version.Minor,
         version.Build,
         version.Revision + 1).ToString();
    // will return 5.0.0.1
}
else
{
    // error handling here
}
+3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    static class Extensions
    {
        public static TOutput[] ToArray<TSource, TOutput>(this IEnumerable<TSource> col, Converter<TSource, TOutput> converter)
        {
            return Array.ConvertAll<TSource, TOutput>(col.ToArray(), converter);
        }
    }

    class Program
    {

        static void Main(string[] args)
        {
            string original = "5.0.0.0";
            int[] tmp = original.Split('.').ToArray<string, int>(new Converter<string, int>(delegate(string s)
            {
                int result;
                return int.TryParse(s, out result) ? result : 0;
            }));

            tmp[tmp.Length - 1]++;
            // re should contain 5.0.0.1
            string re = String.Join(".", tmp);
        }
    }
}
0

, :

  • . a.
  • a . i.
  • i++
  • a[last] = i.ToString()
  • a, .

, : 5.0.0.9 5.0.0.10, 5.0.0.9, .

-1
source

Source: https://habr.com/ru/post/1762312/


All Articles