Figle
asked on
Pulling text from a text file by line and putting into variables to be used and displayed
I am current making a ajax enabled asp.net 2.0 C# web page using Visual studio. net 2005 that pings 13 servers at timed intervals. Everything is finished except i am trying to figure out how to get the webpage to read a text file line by line and store the names of each server in variables as it reads the text file to be used to ping and display the information.
Like you have the variable sHost and the page loads and first name in the file is server1, so it takes server1 and puts it into sHost so PingServer1("sHost") pings server1 and Label1.text = "sHost is up" displays on the page as server1 is up. Then it moves on to second line fo text which is server2 and it repeats the process till all are listed.
Not sure if its possiable the way i described but any ideas are greatly appreciated!
Like you have the variable sHost and the page loads and first name in the file is server1, so it takes server1 and puts it into sHost so PingServer1("sHost") pings server1 and Label1.text = "sHost is up" displays on the page as server1 is up. Then it moves on to second line fo text which is server2 and it repeats the process till all are listed.
Not sure if its possiable the way i described but any ideas are greatly appreciated!
ASKER
thanks! could you please explan this code a bit more please and im not making a console app. its a webpage so couid you explain how to to display the variable in a label instead of console.writeline please?
The console.Writeline was just an example of how you could process each line that you read from the file.
Sorry for confusing you.
the File.ReadAllLines() method reads all the lines in a text file and returns an array of strings. You can iterate through the array to obtain each line of the file and assign them to the required variables.
Sorry for confusing you.
the File.ReadAllLines() method reads all the lines in a text file and returns an array of strings. You can iterate through the array to obtain each line of the file and assign them to the required variables.
ASKER
i think its the pulling each from the arrys thats confusing me, how do i call each label1.text = "{0} is up" + svr just displays {0} is up and the last name on the file
you can do this
label1.txt = svr + "is up"
label1.txt = svr + "is up"
ASKER
no sorry the text display of the variable is fine, just the variable is displaying the last line of text not the first how do i get it to display the first line?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sorry about the long respond time just got back to work, thanks for the help!
using System.IO;
string[] servers = File.ReadAllLines("path_to
foreach (string svr in servers)
{
HostTheServer(svr);
Console.WriteLine("{0} is up", svr);
}