使用索引读取文本文件 [英] Read the textfile using index

查看:78
本文介绍了使用索引读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在文本文件中创建了超过5000行的文本文件。对我来说,当我从300开始在文本框中输入300时,该行应该开始显示。



问候

Balamurugan

Hi,
I have created the textfile inside the text file more than 5000 lines are there.For me when i'm entering 300 in my textbox from 300 the line should start displaying.

Regards
Balamurugan

推荐答案

  private void btnLoad_Click(object sender,RoutedEventArgs e)
        {
//Call my function like this, if you want to read all lines then give 0 instead of 300
            string str = ReadAllLines(@"C:\Bala\Bala.txt", 300);
             MessageBox.Show(str); 
        }

  private static string ReadAllLines(string FilePath, int LineNo)
        {
            try
            {
                //it will get all lines of array.
                string[] AllLinesOfFile = File.ReadAllLines(FilePath);
                
                // To Values from spacific index to onword,

                StringBuilder result = new StringBuilder();
                for ( LineNo = 0; LineNo < AllLinesOfFile.Length; LineNo++)
                {
                    result.Append(AllLinesOfFile[LineNo]+Environment.NewLine);
                }

                // you can use it according to your requirments.

                return result.ToString();
            }
            catch (Exception)
            {
                
                throw;
            }
        }





如有任何问题请告诉我。



if any issue then tell me.


当你写下每个n个字符或当你输入i(i ++)

和i = 300显示时
when you write every n character or when \n you inceremtn i(i++)
and when i=300 display


请检查它。希望这对你有帮助。



Please check it. Hope so it will help you.

private static string ReadAllLines(string FilePath, int LineNo)
       {
           try
           {
               //it will get all lines of file in string array.
               string[] AllLinesOfFile = File.ReadAllLines(FilePath);

               // To Values from spacific index to onword,

               #region Get Spacific values
               if (LineNo < AllLinesOfFile.Length)
               {
                   string[] ResultantArray = null;
                   Array.Copy(AllLinesOfFile, LineNo, ResultantArray, 0, AllLinesOfFile.Length - 1);
                   return string.Join(Environment.NewLine, ResultantArray);
               }
               #endregion

               // you can use it according to your requirments.

               return string.Empty;
           }
           catch (Exception)
           {

               throw;
           }
       }





如果有任何问题让我知道。



Thanx



Muhammad USman



if any issue let me know.

Thanx

Muhammad USman


这篇关于使用索引读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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