Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here my needs.

1
hjgdfghjfg
fsgfggfg
egteg
tghtreghre
wthweghretg
end

2
hjgdfghjfg
fsgfggfg
egteg
tghtreghre
wthweghretg
end


3
hjgdfghjfg
fsgfggfg
egteg
tghtreghre
wthweghretg
end

Assume under this number large amount od data.I have 3 button named 1,2,3 respectively.if I click 1 the data under the 1 upto ed should display in another window.only data in between 1 and end shuld disply.like these if I clik 2 dat under 2 should display..how can i partialy display a file in c#?? plese help its very urgent i cant find a better way
Posted
Comments
Aarti Meswania 27-Mar-15 2:29am    
your question is not clear...
do you have a data in table like below
1 abc...
2 pqr...
3 xyz....
if it is so, then you can display data of first row when user clicks button 1, 2nd row data when user clicks button 2..

please be clear is it a text and you are facing issue to split it?
or facing problem to open up new window which have label docked in and display text whatever it is..
[no name] 27-Mar-15 2:53am    
not in table just in note pad..i wanto display a particular part
BillWoodruff 27-Mar-15 3:31am    
If we do your homework, you will learn nothing. Come back here with specific questions and examples from code you have written.
johannesnestler 27-Mar-15 5:59am    
so what's your problem? reading the text file in? split it? handle window logic? what?

1 solution

If I understood you correctly you want to retrieve from 1 till end, from 2 till end or from 3 till end.
I don't know why but I would expect that you want from 1 to 2, from 2 to 3 or from 3 to end.

Nevertheless the simplest way how you can do this is to just read each line until you find your starting line and when you do then just read the rest of the text file and return that content.
So something like this:
C#
private static string ReadPartialText(string filePath, string startingLine)
{
    using (var reader = new StreamReader(filePath))
    {
        bool startingLineFound = false;

        while (!startingLineFound && !reader.EndOfStream)
            startingLineFound = reader.ReadLine().Equals(startingLine);

        return (startingLineFound) ? reader.ReadToEnd() : string.Empty;
    }
}

And you can use it like this:
C#
string partialText = ReadPartialText("C:\\Sample.txt", "2");

Also just in case I misunderstood you and you actually want to read between 1 and 2 or 2 and 3, etc. in that case instead of using ReadToEnd you will want to continue reading line by line, after you found the starting line you will add those lines into a StringBuilder and when you find an ending line then you will return the StringBuilder's content.
 
Share this answer
 

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