Solved

Pulling text from a text file by line and putting into variables to be used and displayed

Posted on 2008-10-18
8
211 Views
Last Modified: 2010-04-21
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!
0
Comment
Question by:Figle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:CuteBug
ID: 22750595
use the following code

using System.IO;

string[] servers = File.ReadAllLines("path_to_server_file");
foreach (string svr in servers)
{
     HostTheServer(svr);
     Console.WriteLine("{0} is up", svr);
}
0
 

Author Comment

by:Figle
ID: 22750619
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?
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 22750628
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.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:Figle
ID: 22750674
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
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 22750717
you can do this

label1.txt = svr + "is up"
0
 

Author Comment

by:Figle
ID: 22750771
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?
0
 
LVL 16

Accepted Solution

by:
CuteBug earned 500 total points
ID: 22750794
This is because you are looping through all the string and setting the label's value to each string within the loop.
Obviously at the end of the loop, the label will have the last line of text.

Do this instead:

Remove the label and add a ListBox
and with the loop do the following


using System.IO;
 
string[] servers = File.ReadAllLines("path_to_server_file");
foreach (string svr in servers)
{
     HostTheServer(svr);
     listBox1.Items.Add(svr);
}

Open in new window

0
 

Author Closing Comment

by:Figle
ID: 31507510
sorry about the long respond time just got back to work, thanks for the help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question