Click here to Skip to main content
15,117,830 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Posted
Updated 4-Oct-13 22:59pm
v2
Comments
MuhammadUSman1 5-Oct-13 4:30am
   
Please check my solution hope so it'll help you.

if any concern then let my know.

Ask freely.

C#
  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.
   
v5
Comments
Balamurugan1989 5-Oct-13 6:39am
   
Can you edit my code and could you give me the solution.
MuhammadUSman1 5-Oct-13 6:52am
   
Yes i have edit your code. and have give you your required solution.
Please Add as it is in your code. as know my solution is.
it will work for you.
MuhammadUSman1 5-Oct-13 7:02am
   
If you find help full please accept solution, so that can be help full for other readers.
Balamurugan1989 5-Oct-13 7:24am
   
I accepted ur solution its working fine...
Balamurugan1989 5-Oct-13 7:13am
   
Your Code is not executed.....
when you write every n character or when \n you inceremtn i(i++)
and when i=300 display
   
Comments
Balamurugan1989 5-Oct-13 4:05am
   
could u explain me briefly...
MuhammadUSman1 5-Oct-13 4:29am
   
Check my solution.
Please check it. Hope so it will help you.

C#
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;
           }
       }


if any issue let me know.

Thanx

Muhammad USman
   
Comments
Balamurugan1989 5-Oct-13 5:05am
   
wher i need to execute this code???In the Window_load or button click event..
MuhammadUSman1 5-Oct-13 5:59am
   
What you have tried yet. please paste your code, i'll edit and give you proper solution.
Balamurugan1989 5-Oct-13 6:09am
   
I have written the code pl give me the solution
MuhammadUSman1 5-Oct-13 6:01am
   
Did you make any Project for this purpose or not?

Tell in both case, Or give me proper requirements what you want to do?
Balamurugan1989 5-Oct-13 6:06am
   
I have Executed to read the text file using string but now need to execute the index of the text file i.e in text file more than 1000 lines are there using index when i'm selecting 500 means from that the textfile should view.

<pre lang="cs">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Win32;</pre>
namespace TxtFile
{

public partial class MainWindow : Window
{
List<int> lst = new List<int>();
public MainWindow()
{
InitializeComponent();

}

private void Window_Loaded(object sender, RoutedEventArgs e)
{


}

private void button1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
textBox1.Text = op.FileName;
}

private void btnLoad_Click(object sender, RoutedEventArgs e)
{

StreamReader rd = new StreamReader(@"C:\Bala\Bala.txt");
string txt_file = rd.ReadToEnd();
MessageBox.Show(txt_file);
}


}





}

}
MuhammadUSman1 5-Oct-13 6:24am
   
You want to show data on KeyPress or on ButtionClick?
MuhammadUSman1 5-Oct-13 6:25am
   
Check my new solution.
MuhammadUSman1 5-Oct-13 6:33am
   
You can add one more button on that click event call my function and give Your FilePath and No from where to onward you want to display.

Hope so you got my point?
Balamurugan1989 5-Oct-13 7:48am
   
Tks For ur code....
MuhammadUSman1 6-Oct-13 23:46pm
   
w.c
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 ( int i = LineNo; i< AllLinesOfFile.Length; i++))
{
result.Append(AllLinesOfFile[LineNo]+Environment.NewLine);
}

// you can use it according to your requirments.

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

throw;
}
}
   

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