C# 生成固定6位数的文件号

C# 生成固定6位数的文件号,c#,asp.net,.net,for-loop,C#,Asp.net,.net,For Loop,我有一个文件转换实用程序,它在文件中提取一个页面,并以固定的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...

我有一个文件转换实用程序,它在文件中提取一个页面,并以固定的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位将被消除。文件名始终为6位数字,从
000001.jpg
999999.jpg

现在,在我的代码中,我试图读取所有文件。我正在使用一个从1到999999的计数器的for循环来读取文件名。但是当我使用
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";
}
for(int i=0;i<999999;i++){
字符串newFilename=i.ToString(“000000”)+“.jpg”;
}

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

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

for (int i = 0; i < 999999; i++) {
    string newFilename = i.ToString("000000") + ".jpg";
}
for(int i=0;i<999999;i++){
字符串newFilename=i.ToString(“000000”)+“.jpg”;
}
同样,对于读取,可以使用
File.Read(iCount.ToString(“000000”)+“.jpg”)

这应该可以:

string.Format("{0,6}",fileCount).Replace(' ','0')+".jpg";
这应该起作用:

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

我不知道C#,但你能用它填充一下吗<代码>名称.PadLeft(6,'0')
name
01
100
,等等)@pushkin谢谢,我现在就试试。我缺少了技巧/关键字“padding”:)因为我不知道搜索什么来解决我的问题。我不知道C,但你可以用它来填充吗<代码>名称.PadLeft(6,'0')
name
01
100
,等等)@pushkin谢谢,我现在就试试。我缺少技巧/关键字“padding”:),因为我不知道搜索什么来解决我的问题。