Q:

generate file number in fixed 6 digits

I have a file conversion utility which extracts a page in a file and saves in the fixed 6 digit file name like:
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)

etc.
I mean the preceding 0 digits gets eliminated when file number reaches extra digit. The file name will always be 6 digits from 000001.jpg to 999999.jpg
Now, in my code I am trying to read all files. I am using a for loop with a counter from 1 to 999999 to read the file names. But when I use File.Read("00000" + iCount + ".jpg"); it gives error when for loop counter is 10 because then it becomes 7 digits: 0000010.jpg and the file name becomes invalid. And like it becomes 8 digits when the counter is 100.
How do I generate file names in my for loop in the valid manner?

A:

You can generate such file names using the following loop:
for (int i = 0; i < 999999; i++) {
    string newFilename = i.ToString("000000") + ".jpg";
}

Similarly for reading you can use File.Read(iCount.ToString("000000") + ".jpg");

