CodeKeep C# Feed Maggio 7th, 2008
Description: Test whether a computer is alive or not.
do it by ping.
Link: http://www.codekeep.net/snippets/85d2d68d-4cf1-47b2-83a5-44ae2efb5e91.aspx
//Check whether the node is alive or not. If alive, return the IP.
protected static string CheckNodeConnectivity (string hostName)
{
bool nodeIsAlive=false;
string nodeIP = string.Empty;
using (Ping ping=new Ping() )
{
try
{
PingReply reply = ping.Send(hostName, 600); //600 ms
if (reply.Status ==IPStatus .Success )
{
nodeIsAlive = true;
nodeIP = reply.Address.ToString();
}
}
catch (Exception err)
{
// Console.WriteLine("Error has occurred in method CheckNodeConnectivity: " +err.Message );
}
}
return nodeIP;
}

CodeKeep C# Feed Maggio 7th, 2008
->
Description: This code is used to extract any tag from the HTML source.
Link: http://www.codekeep.net/snippets/540a0ee3-d816-42dc-aa6f-4fff23eb23fe.aspx
private string ExtractAnchor(string strAStream)
{
string strAnchor = "";
string arrAnchor = "";
int aIndex = strAStream.ToLower().IndexOf("<a");
while (aIndex != -1)
{
strAStream = strAStream.Substring(strAStream.ToLower().IndexOf("<a"));
strAnchor = strAStream.Substring(0, strAStream.ToLower().IndexOf(">") + ">".Length);
strAStream = strAStream.Substring(strAStream.ToLower().IndexOf(">") + ">".Length);
aIndex = strAStream.IndexOf("<a");
arrAnchor += arrAnchor + ", ";
}
return arrAnchor;
}

CodeKeep C# Feed Maggio 7th, 2008
Description: This code is used to read contents of a given file.
Link: http://www.codekeep.net/snippets/adf5054b-78fb-449f-8423-b693cb6b34fb.aspx
public static string GetFileContents(string FileName)
{
StreamReader sr = null;
string FileContents = null;
try
{
FileStream fs = new FileStream(FileName, FileMode.Open,
FileAccess.Read);
sr = new StreamReader(fs);
FileContents = sr.ReadToEnd();
}
finally
{
if (sr != null)
sr.Close();
}
return FileContents;
}

CodeKeep C# Feed Maggio 7th, 2008
Description: This code is used to get HTML source code of any given URL.
Link: http://www.codekeep.net/snippets/85d76a91-5ab0-4580-9a56-065b971b97b6.aspx
public static string GetHtmlPageSource(string url, string username, string password)
{
System.IO.Stream st = null;
System.IO.StreamReader sr = null;
try
{
// make a Web request
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
// if the username/password are specified, use these credentials
if (username != null && password != null)
req.Credentials = new System.Net.NetworkCredential(username, password);
// get the response and read from the result stream
System.Net.WebResponse resp = req.GetResponse();
st = resp.GetResponseStream();
sr = new System.IO.StreamReader(st);
// read all the text in it
return sr.ReadToEnd();
}
catch (Exception ex)
{
return string.Empty;
}
finally
{
// always close readers and streams
sr.Close();
st.Close();
}
}
