我如何构建下一个文件的字符串变量应该从已知的最后一个文件创建

本文关键字:变量 文件创建 最后一个 字符串 何构建 构建 文件 下一个 | 更新日期: 2023-09-27 18:25:56

这是最后一个文件:

last_file = fi[fi.Length -1].FullName;

在这种情况下,last_file包含目录和文件名:

C: ''Users''bout_000''AppData''Local''mws''My Weather Station''radar_temp_directory''radar010286.gif

现在我想创建下一个文件:

string nextfile = 
File.Copy(combinedTemp, nextfile);

combinedTemp始终是文件名untitled.gif我想将此文件复制到C:''Users''bout_000''AppData''Local''mws''My Weather Station''radar_temp_directory''

如radar010287.gif

我如何构建下一个文件的字符串变量应该从已知的最后一个文件创建

假设fi包含所有名为radarNNNNNN.gif的文件,其中N为0-9,并且列表是有序的,则需要执行以下步骤:

// Obtain the file with the highest number, but only the filename, not the path
string lastFile = fi[fi.Length - 1].Name;
// Take the number part of the filename
string lastFileNumber = lastFile.Substring(5, 6);
// Parse it as int
int lastNumber = int.Parse(lastFileNumber);
// Increase it by one
lastNumber++;
// Build a new filename
string newFileName = string.Format("radar{0:D6}.gif", lastNumber);
// And make it a full path again by prepending the directory name
newFileName = Path.Combine(radarImagesDirectory, newFileName);

当然,当目录中没有文件时,您必须包含一些错误处理和代码才能使其工作。