Writing to a text file C#

Anoushka Seechurn picture Anoushka Seechurn · Oct 3, 2013 · Viewed 9.5k times · Source

I have the following values

A = 1, 
B = (NULL of 7 characters), 
C = denimRocks , 
D = Yes789, 
E = (NULL of 2 characters), 
F = ATR.

I want to write these to a line of a textfile with A starting at position 1 of the line.B starting at position 2, C at position 8 and so on.

I want to show blank spaces for the nulls. I tried using streamwriter but I cant get to acheive what I want to do.

Help please.

Answer

Saritha.S.R picture Saritha.S.R · Oct 3, 2013

Try like this:

 string a=string.Empty;
    StreamWriter yourStream = null;
    protected void Page_Load(object sender, EventArgs e)
    {
            yourStream = File.CreateText("D:\\test1.txt"); // creating file
            a = String.Format("|{0,1}|{1,2}|{2,7}|......", "A", "B", "",......); //formatting text based on poeition
            yourStream.Write(a+"\r\n");                        
            yourStream.Close();
        }