我正在使用C#在VS2005中开发Windows应用程序.在我的项目中,我生成dll并将它们存储在一个目录中.dll将命名为TestAssembly1,TestAssembly2,TestAssembly3等.
因此,请考虑以上三个dll是否在目录中.下次用户使用我的项目时,我需要生成像TestAssembly4,TestAssembly5等dll.
那么如何在文件夹中存储dll的计数并在下次使用项目时递增它们?
该目录甚至可以包含dll以外的文件.那我该怎么做呢?
Mar*_*ell 11
就个人而言,我会使用二进制搜索来查找下一个程序集......
并且不使用16和32之间的二进制搜索:
所以使用TestAssembly30.dll
这样就无需单独保存计数,因此即使删除所有文件也能正常工作 - 而二进制搜索意味着您的性能不会太差.
未经测试,但如下所示; 另请注意,基于文件存在的任何内容都会立即成为竞争条件(尽管通常非常小):
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);
}
Run Code Online (Sandbox Code Playgroud)
您只需使用 Directory.GetFiles,传入要返回的文件的模式:
http://msdn.microsoft.com/en-us/library/wz42302f.aspx
string[] files = Directory.GetFiles(@"C:\My Directory\", "TestAssembly*.dll");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3257 次 |
| 最近记录: |