Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a windows application. I want to insert header line in text file. In this below code I have created a folder in that folder I have created a text file. Now I want to write first line in that I am getting problem. Is there any mistake in my code.

if ( !( System.IO.Directory.Exists( "C:\\TSOSM" ) ) )
   System.IO.Directory.CreateDirectory( "C:\\TSOSM" );
else if ( ( newmarketname.Text == "" ) )
   MessageBox.Show( "Please write Market Name to Proceed." );
      if ( !System.IO.Directory.Exists( ( "C:\\TSOSM\\" + ( newmarketname.Text + "" ) ) ) )
         System.IO.Directory.CreateDirectory( ( "C:\\TSOSM\\" + ( newmarketname.Text + "" ) ) );
      string FILE_NAME = @"C:\TSOSM\" + ( newmarketname.Text + ( "\\" + ( newmarketname.Text + ".txt" ) ) );
      System.IO.File.Create( FILE_NAME );
      int i;

      string[ ] aryText = "<1>,<2>,<3>,<4>,<5>,<6>";
      System.IO.StreamWriter objWriter = new System.IO.StreamWriter( FILE_NAME, false );
      for ( i = 0; ( i <= 0 ); i++ )
         objWriter.WriteLine( aryText[ i ] );
      objWriter.Close( );
      MessageBox.Show( "created successfully" );
   catch ( Exception ex )
      MessageBox.Show( ex.Message );

[edit]indexation adjusted and 3 "\\ " bolded to avoid escaped quotes interpretation and incorrect code format[/edit]
Updated 5-Jan-14 0:47am
CPallini 5-Jan-14 6:15am    
What is the problem?
hussain548 5-Jan-14 6:18am    
line is not inserting
Karthik_Mahalingam 5-Jan-14 6:18am    
provide more information.
hussain548 5-Jan-14 6:22am    
text file is created but line is not inserted. may be problem in array declaration. I know the way is right but what's the error.
Karthik_Mahalingam 5-Jan-14 6:28am    
chk my solution.

1 solution

array should be declared like this..

in the for loop you have hardcoded 0 value, it should be array length..
string[ ] aryText = "<1>,<2>,<3>,<4>,<5>,<6>";       
 string[] aryText= { "<1>","<2>","<3>","<4>","<5>","<6>"};

    System.IO.StreamWriter objWriter = new System.IO.StreamWriter(FILE_NAME, false);
    for (i = 0; (i <= 0); i++)
for (i = 0; (i <= aryText.Length); i++)
Share this answer
hussain548 5-Jan-14 6:34am    
Thanks for reply Karthik sir, I appreciate that. but I am getting this error after changing array diclaration
the process cannot be access the file'c:\TSO0SM\abc\abc.txt' because it is using by another process.
process not entering in to for loop.
Karthik_Mahalingam 5-Jan-14 6:39am    
because the text file is opened somewhere outside the application.
close all the text file. and try running it...
hussain548 5-Jan-14 6:45am    
No sir, any text file is not opened. at line of stream writer any error is there, because after that it was not moving to for loop.
Nelek 5-Jan-14 6:49am    
Are you sure you are closing the file everytime you do something with it? It is not a must you have it open in the notepad, it can be open inside your own application
hussain548 5-Jan-14 6:54am    
yes sir, now I restarted my pc and run only mu app then also same error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900