tags:

views:

66

answers:

3
//Introduction
Hey, Welcome.....
This is the tutorial 
//EndIntro

//Help1
Select a Stock
To use this software you first need to select the stock. To do that, simply enter the stock symbol in the stock text-box (such as "MSFT"). 
To continue enter "MSFT" in the stock symbol box.
//EndHelp1

//Help2

Download Stock Data
Next step is to to download the stock data from the online servers. To start the process simply press the "Update" button or hit the <ENTER> key.
After stock data is downloaded the "Refresh" button will appear instead. Press it when you want to refresh the data with the latest quote.
To continue make sure you are online and press the "Update" button
//EndHelp2

First time I want to display the content between //Introduction and //EndIntro then second time the content between //Help1 and //EndHelp1 and so on.

+3  A: 

That's a very open-ended question - what sort of file? To read binary data from it you'd usually use:

using (Stream stream = File.OpenRead(filename))
{
    // Read from the stream here
}

or

byte[] data = File.ReadAllBytes(filename);

To read text you could use any of:

using (TextReader reader = File.OpenText(filename))
{
    // Read from the reader
}

or

string text = File.ReadAllText(filename);

or

string[] lines = File.ReadAllLines(filename);

If you could give more details about the kind of file you want to read, we could help you with more specific advice.

EDIT: To display content from an RTF file, I suggest you load it as text (but be careful of the encoding - I don't know what encoding RTF files use) and then display it in a RichTextBox control by setting the Rtf property. Make the control read-only to avoid the user editing the control (although if the user does edit the control, that wouldn't alter the file anyway).

If you only want to display part of the file, I suggest you load the file, find the relevant bit of text, and use it appropriately with the Rtf property. If you load the whole file as a single string you can use IndexOf and Substring to find the relevant start/end markers and take the substring between them; if you read the file as multiple lines you can look for the individual lines as start/end markers and then concatenate the content between them.

(I also suggest that next time you ask a question, you include this sort of detail to start with rather than us having to tease it out of you.)

EDIT: As Mark pointed out in a comment, RTF files should have a header section. What you've shown isn't really an RTF file in the first place - it's just plain text. If you really want RTF, you could have a header section and then the individual sections. A better alternative would probably be to have separate files for each section - it would be cleaner that way.

Jon Skeet
@Harikrishna - RTF is largely a text-based format, so `ReadAllText` will get the raw RTF, but that does **not** make it any easier to understand the contents... perhaps what you want is an RTF parser...
Marc Gravell
@Mr Jon Skeet Can you say that how to display part of the file
Harikrishna
@Harikrishna: I've already outlined how you'd do it: load the whole file, find the start and end markers, take the text between them, and then set the `Rtf` property with that substring.
Jon Skeet
@Mr Jon Skeet You mean that load the whole file into the richtextbox by richtextbox.Loadfile("path") function.Then it will display whole the content of the file in the richtextbox.
Harikrishna
@Jon Skeet: I don't think it's quite that simple. You need a header too. See: http://msdn.microsoft.com/en-us/library/aa140298%28office.10%29.aspx for the RTF specs.
Mark Byers
@Jon Skeet Can you explain in detail please how to load the whole file, find the start and end markers, take the text between them, and then set the Rtf property with that substring.
Harikrishna
@Mark: Interesting point. It doesn't look like it's really an RTF file to start with, to be honest - it looks like plain text to me... @Harikrishna: No, I'm afraid I'm too busy to give more explanation than I've already given. I've already explained which method calls will load the whole file, and pointed to IndexOf and Substring. You're going to have to do some work yourself. Try what I've suggested, and if you get stuck show us what you've already done. If we just give you the complete code you'll never really learn anything.
Jon Skeet
Yes, sir it is..But I have tried to do that but I did not get the solution.
Harikrishna
If you've tried, you should be able to show *what* you've tried. That way we can help you to *learn*.
Jon Skeet
@sir Jon Skeet Thank You sir
Harikrishna
+1  A: 

Not sure I understand your question correctly. But you can read and write content using System.IO.StreamReader and StreamWriter classes

string content = string.Empty;
using (StreamReader sr = new StreamReader("C:\\sample.txt"))
{
    content = sr.ReadToEnd();
}
using (StreamWriter sw = new StreamWriter("C:\\Sample1.txt"))
{
    sw.Write(content);
}
Anuraj
+1  A: 

Your question needs more clarification. Look at System.IO.File for many ways to read data.

The easiest way of reading a text file is probably this:

string[] lines = File.ReadAllLines("filename.txt");

Note that this automatically handles closing the file so no using statement is need. If the file is large or you don't need all lines you might prefer to reading the text file in a streaming manner:

using (StreamReader streamReader = File.OpenText(path))
{
     while (true)
     {
         string line = streamReader.ReadLine();
         if (line == null)
         {
             break;
         }
         // Do something with line...
     }
}

If the file contains XML data you might want to open it using an XML parser:

XDocument doc = XDocument.Load("input.xml");
var nodes = doc.Descendants();

There are many, many other ways to read data from a file. Could you be more specific about what the file contains and what information you need to read?

Update: To read an RTF file and display it:

richTextBox.Rtf = File.ReadAllText("input.rtf");
Mark Byers
richTextBox.Rtf = File.ReadAllText("input.rtf");
Mark Byers
Is //start and //end text found inside the rtf text? Do they appear on lines of their own? What if they appear more than once? What else do you need to do apart from this?
Mark Byers
Does "richTextBox.Rtf = File.ReadAllText("input.rtf");" correctly display the entire text, without the filtering? If not, what error do you get?
Mark Byers
then lines between //control1 and //endControl1
Harikrishna
No it does not give the error but I dont want to display entire text,but I want to display particular lines between some define words.
Harikrishna
You might want to look at an RTF parser like http://nrtftree.sourceforge.net/
Mark Byers