From a C# console application, how can I create a variable that will always have a fixed length 8 position value with leading zeroes?

From a C# console application, how can I modify the following code so that the value for the variable MyGlobals.intsequence will always be a fixed length 8 position field value with leading zeroes?

I used the variable MyGlobals.intsequence as part of file names and increment its value by 1 each time through a loop so that I can assign file names as follows with the 4th qualifier as a sequence number, 8 positions in length:


public static class MyGlobals
        public static uint intsequence = 00000000;      

static void Main()
             MyGlobals.BASE_FILE_Name = "C.C.PAE" + "." + MyGlobals.intsequence + "." + DateTime.Now.ToString("yyyyMMdd.HHmmss");
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
Try integer's ToString and pass the number of leading zeroes you want, by specifying the length of the string, something like:
 int i = 1;
string str = i.ToString("D8");

This results in output 00000001

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
If it must be 8 digits with leading zeros then you have two options.
A string.
An integer type that you MUST format to a string before you use it.
Why?  Number types such as integer do NOT support leading/trailing zeros.
Proof of concept using Karrtik's recommendation:
using System;

namespace EE_Q28735542
	class Program
		static int sequence = 0;
		static void Main(string[] args)
			for (int i = 0; i < 10; i++)
				Console.WriteLine(string.Format("C.C.PAE.{0}.{1:yyyyMMdd.HHmmss}", sequence.AsSequence(), DateTime.Now));

	static class Extensions
		public static string AsSequence(this int value, int leadingZeros = 8)
			return value.ToString(string.Format("D{0}", leadingZeros >= 1 ? leadingZeros : 0));

Open in new window

Produces the following output -Capture.JPG-saige-
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.