Hi this is small class I made to learn about files and strings in C#, it read a text file with keys and valules you chould use this to make your program support language files. anyway hope this may help someone
Class Code
Example codeCode:using System; using System.Collections; using System.Linq; using System.Text; using System.IO; namespace StrList { public class cStrList { // string File; Hashtable ht = new Hashtable(); //Consturctor public cStrList(string Filename) { //Filename to open string sLine = ""; string sKey = ""; string sValue = ""; int s_pos = 0; //File = Filename; //Create file stream object StreamReader sr = new StreamReader(Filename); while (sLine !=null) { //Read the line sLine = sr.ReadLine(); if (sLine != null) { if (Convert.ToBoolean(sLine.Length)) { s_pos = sLine.IndexOf("="); if (s_pos > -1) { //Extract Key. sKey = sLine.Substring(0, s_pos).Trim(); //Extract Value. sValue = sLine.Substring(s_pos + 1, sLine.Length - s_pos - 1).Trim(); //Test if key is already added if (!ht.ContainsKey(sKey)) { //Store the key and value into hashtable. ht.Add(sKey, sValue); } } } } } sr.Close(); } public string ReadValue(int Key) { //This returns a string from a given key. try { //Try and resturn value return ht[Key.ToString()].ToString(); } catch { //Just return return ""; } } } }
Code:private void button1_Click(object sender, EventArgs e) { //Example File //100=String List Example //101=About //102=E&xit //Code example string Filename; Filename = "C:\\cd\\test.txt"; if (!System.IO.File.Exists(Filename)) { MessageBox.Show("File Not Found\n" + Filename,"File Not Found", MessageBoxButtons.OK,MessageBoxIcon.Error); } else { cStrList cLst = new cStrList(Filename); //Set form text properties ActiveForm.Text = cLst.ReadValue(100); cmdAbout.Text = cLst.ReadValue(101); cmdExit.Text = cLst.ReadValue(102); } }


Reply With Quote