在文本文件中插入行 [英] insert line in text file

查看:77
本文介绍了在文本文件中插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Windows应用程序。我想在文本文件中插入标题行。在下面的代码中,我在该文件夹中创建了一个文件夹,我创建了一个文本文件。现在我想写第一行,因为我遇到了问题。我的代码中是否有任何错误。



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." );
}
else
{
   try
   {
      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 );
   }
}





指数调整和3\\\ \\加粗以避免转义引号解释和错误的代码格式[/ edit]



[edit]indexation adjusted and 3 "\\ " bolded to avoid escaped quotes interpretation and incorrect code format[/edit]

推荐答案

数组应该像这样声明.. <在$ for循环中,你有硬编码的0值,它应该是数组长度..

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++)
{
objWriter.WriteLine(aryText[i]);


这篇关于在文本文件中插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆