Category Archives: C#

Reading a file with C#

      No Comments on Reading a file with C#

Reading a text file line by line in C#. Dont forget to include using System.IO; StreamReader myReader = new StreamReader(@”c:\test.txt”); string line = “”; while (line != null) { line = myReader.ReadLine(); Console.WriteLine(line); } You can read all the text within a file using the ReadAllText function. string filename =… Read more »

Explicit Conversion of a string to Integer using c#

Explicit Conversion of a string to Integer, sometimes known as cast. string myString = “7”; int myInt = int.Parse(myString); Console.WriteLine(myInt); The above code will fail if the string can not be converted to a integer. For example if myString=”Seven”, the code would fail. To avoid this problem use TryParse. string… Read more »

Get Web Page Content as a String with C#

      2 Comments on Get Web Page Content as a String with C#

This function dowloads the html content of a webpage and returns the content as a string. public static string GetWebPageAsString(string url) { HttpWebRequest httpWebRequest =(HttpWebRequest)WebRequest.Create(url); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); Stream stream = httpWebResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(stream, Encoding.ASCII); return streamReader.ReadToEnd(); }

Create a autocompleting textbox using C#

      2 Comments on Create a autocompleting textbox using C#

The code example below creates an autocompleting textbox, with the possible values Andy, Andrew, Dylan and Kyle. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace autoComplete { public partial class Form1 : Form { AutoCompleteStringCollection nameCollection = new AutoCompleteStringCollection(); public Form1()… Read more »

Create arraylist

      1 Comment on Create arraylist

Create an ArrayList and add three elements. using System.Collections; class Program { static void Main() { ArrayList list = new ArrayList(); list.Add(“One”); list.Add(“Two”); list.Add(“Three”); } }