c# - 获取数组中的最高数字,然后在该数组中添加一个新值并使用前面的 +1 不起作用 - C#
问题描述
我正在制作一个文件名管理器,它在文件夹中搜索文件,在文件名中提取一个数字,将它们放入一个数组中,按降序对它们进行排序。然后我获得最高数字并将其加 1,然后将其用作下一个文件名。
例子:
文件夹 A 中的文件:file1.txt file2.txt file3.txt
如果我创建一个新文件,它应该自动命名为 file4.txt。
但这不是正在发生的事情
考虑:
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;
}
}
使用时输出:
- 文件1.png
- 文件18.png
- file18.png(覆盖文件)
- ...
预期输出:
- 文件1.png
- 文件2.png
- 文件3.png
- ...
我该如何解决这个问题,使其按应有的方式运行。
如果代码很难看,请原谅它,我只是想让它先工作。
解决方案
注意:这不是 /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;
}
}
它扫描每个可能的文件不应该成为性能问题,因为该程序并不意味着扫描大量文件,除非用户真的滥用它。
感谢那些在评论中指出问题和优化的人。
推荐阅读
- javascript - Three.js - 如何替换 Object3D 的模型?
- python - 如何从网页内的链接中提取数据,然后将它们添加到已经存在的 csv 文件中?
- javascript - 无法在 L.Routing.control 中使用我的标记的 latLng
- php - 如何在 laravel 中更新数组
- sql-server - 在 SQL Server 的决策中避免使用聚合函数
- prometheus - prometheus 规则评估是如何工作的?
- c++ - `bool n;` `n++;` 无效但是 `n=n+1;` 或 `n=n+3` 这样的东西有效,这有什么意义?
- internationalization - i18n : 货币本地化,无需通过区域设置 ISO 代码
- android - Flutter 无法在 android 模拟器上运行 => irebaseCloudMessagingPluginRegistrant
- azure-data-factory - Azure 数据工厂 - 只有 1 条记录时集合引用失败