Harikrishna
Harikrishna

Reputation: 4305

Accessing the content of the file

//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.

Upvotes: 0

Views: 285

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1503290

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.

Upvotes: 3

Mark Byers
Mark Byers

Reputation: 839054

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");

Upvotes: 1

Anuraj
Anuraj

Reputation: 19618

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);
}

Upvotes: 1

Related Questions