Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# C#4.0
Hi,
i have two textboxes.
 
I have a text file that has list of file paths for different scripts used on our network.
the file is setup like this:
 
C:\Scripts\dnsflush.ps1
C:\Scripts\DFQ.ps1
D:\N\Exchange\Netservicestop.ps1
D:\AD\scripts\ugroups.ps1
 
i want to only read the lines "C:\Scripts\dnsflush.ps1" -line0 and "D:\N\Exchange\Netservicestop.ps1" -line2 and then iwant to print the line to textbox on the form as a string.
 
	
	foreach (string line in File.ReadLines("c:\\file.txt"))
	{
	    textbox1.text = (line0);
            textbox2.text = (line2);
	}
 
i have no idea where to start any help would be great Smile | :)
Posted 14-Nov-12 14:53pm
SIFNOk816
Edited 14-Nov-12 15:06pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

One way of doing it is;
 
string filename = "yourtextfilepath.txt";
 
if (File.Exists(filename))
{
    string[] lines = File.ReadAllLines(filename);
    Console.WriteLine(lines["the line number that you want to print - 1"]);
}
 
If you dont want to read all the lines here is another solution but you have to read all the previous line till to the desired line number:
 
string GetLine(string fileName, int line)
{
   using (var sr = new StreamReader(fileName)) {
       for (int i = 1; i < line; i++)
          sr.ReadLine();
       return sr.ReadLine();
   }
}
 
Good luck,
OI
  Permalink  
v2
Comments
SIFNOk at 14-Nov-12 20:40pm
   
Thank you soo much OI, Your first solution worked best! as all i need to do is read the line as string :) i appericate it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 400
1 Maciej Los 222
2 OriginalGriff 213
3 BillWoodruff 130
4 Zoltán Zörgő 85
0 OriginalGriff 7,969
1 DamithSL 6,139
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,309
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100