首页 > 解决方案 > 获取数组中的最高数字,然后在该数组中添加一个新值并使用前面的 +1 不起作用 - C#

问题描述

我正在制作一个文件名管理器,它在文件夹中搜索文件,在文件名中提取一个数字,将它们放入一个数组中,按降序对它们进行排序。然后我获得最高数字并将其加 1,然后将其用作下一个文件名。

例子:

但这不是正在发生的事情

考虑:

CheckDir() 只是找到一个特殊文件夹的位置并返回它。(即 C:/Users/Name/Appdata)

前缀是用户设置的字符串,我们将在本例中使用“文件”。

public static string ProcessPath()
{

    string path = CheckDir();

    if (path != null) //if there is a path
    {

        string[] files = Directory.GetFiles(path, prefix + "*.png");
        File.WriteAllText(Path.Combine(mainpath, "filescheck.txt"), string.Join(",", files)); /////////////


        if (files.Length != 0) //if there is files that match the prefix then -->>>
        {
            File.WriteAllText(Path.Combine(mainpath, "filesnotnull.txt"), "hi"); //////

            Nullable<int> highest = null;
            var fileNames = files;
            var numberList = new List<int>();
            int number = 0;
            string numtouse = "";

            foreach (var name in fileNames)
            {
                string m = Regex.Match(name, @"\d+").Value;
                var isNumber = Int32.TryParse(m.ToString(), out number);

                if (isNumber) numberList.Add(number);
            }

            highest = numberList.OrderByDescending(x => x).FirstOrDefault();

            var result = (highest.ToString() ?? null);
            numtouse = (highest + 1).ToString();

            if (result != null)
            {
                string filename = prefix + numtouse + ".png";
                string givepath = Path.Combine(path, filename);
                return givepath;
            } else
            {
                return null;
            }

        } else // if there is no files with the prefix then -->>>
        {

            string filename = prefix + "1.png";
            string givepath = Path.Combine(path, filename);
            return givepath;

        }
    } else //retry if no path
    {
        MoozSS.TakeSS();
        return null;

    }
}

使用时输出:

预期输出:

我该如何解决这个问题,使其按应有的方式运行。

如果代码很难看,请原谅它,我只是想让它先工作。

标签: c#.netvisual-studio

解决方案


注意:这不是 /this 解决问题 / 和更多 /this 避免问题 /。

这是我最终使用的代码。

 public static string ProcessPath()
        {
            string path = CheckDir();

            if (path != null)//if there is a path
            {

                string[] files = Directory.GetFiles(path, prefix + "*.png");

                if (files.Length != 0)//if there is files that match the prefix then -->>>
                {
                    int count = 2;

                    while(File.Exists(Path.Combine(path, (prefix + count + ".png"))))
                    {
                        count = count + 1;
                    }

                    string filename = prefix + count + ".png";
                    string givepath = Path.Combine(path, filename);
                    return givepath;


                }
                else // if there is no files with the prefix then -->>>
                {

                    string filename = prefix + "1.png";
                    string givepath = Path.Combine(path, filename);
                    return givepath;

                }

            }
            else //retry if no path
            {

                MoozSS.TakeSS();
                return null;

            }
        }

它扫描每个可能的文件不应该成为性能问题,因为该程序并不意味着扫描大量文件,除非用户真的滥用它。

感谢那些在评论中指出问题和优化的人。


推荐阅读