Hesham Hassanein

Hesham Hassanein

  • NA
  • 140
  • 17k

How to put the first line of text file in the end position

Sep 21 2016 12:36 PM
I have a streamwriter. I wrote into a text file all lines but I want to put the first line in the last position of the text file. After writing the stream, i want to switch first line to the last but the first line should not be left blank but to shift the preceeding lines up and then write the stream again. How can i Hold the first line in a variable and start writing from second line and in which part of the code. Thanks in Advance
 
 
  1. using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath))  
  2.            {  
  3.                string[] buffer = new string[count];  
  4.                
  5.                while (!(lines = reader.ReadLine()).Contains("Records"))  
  6.                {  
  7.   
  8.                      

  9.                    for (int i = 0; i < count - 1; i++)  
  10.                    {  
  11.   
  12.                        string[] fields = lines.Split('|');  
  13.   
  14.   
  15.                        Result[i, 0] = "";  
  16.                        
  17.                            Result[i, 1] = fields[1];                                 
  18.                            Result[i, 2] = " ";  
  19.  
  20.                            Result[i, 3] = xx;  
  21.   
  22.                            Result[i, 4] = "";  
  23.                            Result[i, 5] = fields[4];  
  24.                            Result[i, 6] = "";  
  25.                            Result[i, 7] = fields[0];                                
  26.                            Result[i, 8] = "";  
  27.   
  28.                            if (fields[15].Contains("PASS"))  
  29.                            {  
  30.                                Result[i, 9] = "P";  
  31.                            }  
  32.                            else if (fields[15].Contains("FAIL"))  
  33.                            {  
  34.                                Result[i, 9] = "F";  
  35.                            }                                                          
  36.                            Result[i, 10] = "";  
  37.                            Result[i, 11] = "";                                                   
  38.                            Result[i, 12] = "";  
  39.                            Result[i, 13] = "";  
  40.                            Result[i, 14] = "";  
  41.   
  42.                            }  
  43.   
  44.                        }  
  45.   
  46.   
  47.   
  48.                        else if (fields.Length == 7)  
  49.                        {  
  50.   
  51.   
  52.                            Result[i, 1] = "";  
  53.                       
  54.                            Result[i, 2] = "";  
  55.                              
  56.                              
  57.                            Result[i, 3] = fields[0];                                                       //Test Code  
  58.                            Result[i, 4] = "1";                                                                 // Test Channel  
  59.                            Result[i, 5] =fields [4];                                                           // Test Value  
  60.                            Result[i, 6] = "0";                                                             //Test Frequency  
  61.   
  62.                            if (fields[5].Contains("PASS"))   
  63.                            {   
  64.                                Result[i, 7] = "P";   
  65.                            }  
  66.                            else if (fields[5].Contains("FAIL"))   
  67.                            {  
  68.                                Result[i, 7] = "F";   
  69.                            }   
  70.                              
  71.                              
  72.                            
  73.                            Result[i, 8] = "0";                             // Test Volt
  74.  
  75.   
  76.                            
  77.                                    Result[i, 9] = fields[1];  
  78.   
  79.                                    Result[i, 10] = "0.0";  
  80.   
  81.                                }  
  82.   
  83.   
  84.                            }  
  85.   
  86.                            Result[i, 11] = "0";                       
  87.                            Result[i, 12] = "1";                       
  88.                            Result[i, 13] = "1";                      
  89.                            Result[i, 14] = "0";                   
  90.                            Result[i, 15] = Profile_Index.ToString();  
  91.   
  92.                        }  
  93.   
  94.                        result = ("PATS_TEST" + "," + Result[i, 1] + "," + Result[i, 2] + "," + Result[i, 3] + "," + Result[i, 4] + "," + Result[i, 5] + "," + Result[i, 6] + "," + Result[i, 7] + "," + Result[i, 8] + "," + Result[i, 9] + "," + Result[i, 10] + "," + Result[i, 11] + "," + Result[i, 12] + "," + Result[i, 13] + "," + Result[i, 14] + "," + Result[i, 15]);  
  95.   
  96.                          
  97.                          
  98.                        if (fields[0].Contains("END-OF-LINE"))  
  99.                        {  
  100.                            result = ("END-OF-LINE");  
  101.                        }  
  102.   
  103.   
  104.                    }  
  105.   
  106.   
  107.                    Profile_Index++;  
  108.   
  109.   
  110.                    // Console.WriteLine(result);  
  111.   
  112.                    file.WriteLine(result);  
  113.                     
  114.   
  115.                     
  116.                        }  

Answers (2)