我正在使用 C# 在 VS2005 中开发 Windows 应用程序。在我的项目中,我生成 dll 并将它们存储在一个目录中。这些 dll 将被命名为 TestAssembly1、TestAssembly2、TestAssembly3 等。
所以考虑上面三个dll是否在目录中。下次用户使用我的项目时,我需要生成 TestAssembly4、TestAssembly5 等 dll。
那么如何存储文件夹中dll的数量并在下次使用项目时递增它们呢?
该目录甚至可以包含 dll 以外的文件。那么我该怎么做呢?
就我个人而言,我会使用二分搜索来查找下一个程序集......
- 开始 n=1
- TestAssembly1.dll 存在吗? (是的)
- TestAssembly2.dll 存在吗? (是的)
- TestAssembly4.dll 存在吗? (是的)
- TestAssembly8.dll 存在吗? (是的)
- TestAssembly16.dll 存在吗? (是的)
- TestAssembly32.dll 存在吗? (不)
并且在 16 和 32 之间不使用二分查找:
- TestAssembly24.dll 存在吗? (是的)
- TestAssembly28.dll 存在吗? (是的)
- TestAssembly30.dll 存在吗? (不)
- TestAssembly29.dll 存在吗? (是的)
所以使用TestAssembly30.dll
这避免了单独保存计数的需要,因此即使您删除所有文件它也会工作 - 并且二分搜索意味着您的性能不会太差。
未经测试,但如下所示;另请注意anything基于文件存在立即成为一种竞争条件(尽管通常非常小):
static string GetNextFilename(string pattern) {
string tmp = string.Format(pattern, 1);
if (tmp == pattern) {
throw new ArgumentException(
"The pattern must include an index place-holder", "pattern");
}
if (!File.Exists(tmp)) return tmp; // short-circuit if no matches
int min = 1, max = 2; // min is inclusive, max is exclusive/untested
while (File.Exists(string.Format(pattern, max))) {
min = max;
max *= 2;
}
while (max != min + 1) {
int pivot = (max + min) / 2;
if (File.Exists(string.Format(pattern, pivot))) {
min = pivot;
}
else {
max = pivot;
}
}
return string.Format(pattern, max);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)