0

I'm trying to select a set of lines and process each line separately in a text document using c# language. How can i get separate lines to process? I tried these codes and got struck. Can anyone please help me with this?

EnvDTE.DTE dte = MyPackage.MyPackagePackage.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
EnvDTE.TextSelection text = (dte.ActiveDocument.Selection as EnvDTE.TextSelection);
Ricky
  • 2,323
  • 6
  • 22
  • 22

3 Answers3

2

TextSelection interface has got Text property which you can use as string in C#. Further you can split the string to retrieve the lines.

Alternatively TextSelection interface has additional property called TextRanges which has numeric indexers to access each line.

Kajal Sinha
  • 1,565
  • 11
  • 20
0

Have a look at this Link form MSDN.

You can use Startpoint and EndPoint for your job.

Also this Link link might be useful to Loop through all the lines from your selection.

H. Mahida
  • 2,356
  • 1
  • 12
  • 23
-2

If you are reading from a text file this code will help you:

string fileToRead = "D:\\temp.txt";    // Temp.txt is the file to read

if (File.Exists(fileToRead))
{
    StreamReader reader = new StreamReader(fileToRead);
    do
    {
         textBox1.Text += reader.ReadLine() + "\r\n";    // Read each line and pass it to the TextBox1

    } while (reader.Peek() != -1);
    reader.Close(); // Close the file 
}
Pabinator
  • 1,601
  • 1
  • 21
  • 25
  • Hello Pabinator, My problem is i dont use text document. I have to process on the selected text in a document. I can get that selected lines from TextSelection class. When i pass this selected text to a stream reader it doesnt work as expected – Satheesh Kurunthiah Jun 27 '14 at 10:53
  • How does this reply relates to EnvDTE? This cannot be an answer! – Kajal Sinha Jun 27 '14 at 11:15