繁体   English   中英

生成固定6位数字的文件号

[英]generate file number in fixed 6 digits

我有一个文件转换实用程序,可将页面提取到文件中并保存为固定的6位数字文件名,例如:

000001.jpg  -  first page of file
000002.jpg  -  second page of file
...
000010.jpg  -  tenth page of file
000011.jpg  -  eleventh page of file
000100.jpg  -  hundredth page of file
000101.jpg  -  and so on...
...
001000.jpg
001001.jpg
...
...
999999.jpg  -  upto the maximum 999999th page. (6 digits max)

等等

我的意思是当文件号达到多余位数时,前面的0位数字将被消除。 文件名从000001.jpg999999.jpg始终为6位数字

现在,在我的代码中,我试图读取所有文件。 我正在使用for循环,其计数器从1到999999,以读取文件名。 但是当我使用File.Read("00000" + iCount + ".jpg"); 当for循环计数器为10时,它将给出错误,因为它变为7位数字: 0000010.jpg ,文件名变为无效。 当计数器为100时,它变成8位数字。

如何以有效方式在for循环中生成文件名?

您可以使用以下循环生成此类文件名:

for (int i = 0; i < 999999; i++) {
    string newFilename = i.ToString("000000") + ".jpg";
}

同样,对于读取,您可以使用File.Read(iCount.ToString("000000") + ".jpg");

这应该工作:

string.Format("{0,6}",fileCount).Replace(' ','0')+".jpg";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM