Read Text File(.txt) and Use in Windows Desktop Application and Web Application:

Question:
Hi Friends, I want to read .txt file in my Application Program. Right this time , I am concerning in desktop application in Windows Application Form in C#. I want to read world country list, listed in a notepad document(.txt) , and add each country name to Checkbox component so that I could give users to browse their country name. It will be also great if anyone tell me how to add this to asp.net/c# website dropdownlist component. Please guide me
Desktop Application:
First create a text file with country name. One line one country (I feel comfortable one name in one line). like
Nepal
China
Canada
Australia
-------------------------
Note: This text file should locate where your .exe file is created.(Only for this demo you can keep any where you want) Then

Add one button to load country list from file and combobox to display result.

When button is clicked write this code.
--------------------------
string countriesList = File.ReadAllText(Application.StartupPath+"//Countries.txt");
string[] countries = countriesList.Split('\n');
foreach (var country in countries)
{
comboBox1.Items.Add(country);
}
------------------------------
Note: Countries.txt is a text file that countain country name as I stated above. If you prefer to write country name like any of the following ways
Nepal, China, Canada, Australia,................
Nepal;China;Canada,Australia;............
Or any way with one special character that separate two country
that special character is used in splitting list in above code. I have used new line so I have used '\n' but if you use any other character then second line code becomes
string[] countries=countriesList.Split('SpecialCharacter')

_--------------------------------------------------------------
For Web application you can not use Application.StartupPath
instead of this you have to use Server.MapPath("Location of File")
Other code remains same.
Bhuban Shrestha's photo.
 
 
Explanation:
And I forgot to say you have to use System.IO .

File.ReadAllText("Path of File")

=> This read all text in a file specified .
=> So how to specify file path. Well you can specify by its absolute path like "C:\\ProgramFiles\\MyProgram\\ABC.txt"
you can remove double \\ by using @ sign in front of string.
Another way is using Application.StartupPath. This gives path (location of your exe file) where ever it is no matter C drive, D drive, no matter. then with + sign I concatinate Countries.txt because my country list containing file is at same location where my application starts.

CountryList.Split function split string into array specified by split character. I have explained about it above.

The with foreach loop I added those array value to combobox.

Remaining concept is Server.MapPath to understand this you have to know web application architecture first. But in simple answer this is equivalent as Application.mapPath.
 
Web Application Example:
 सन्जीव भुसाल's photo.
This is my Signup.aspx page , where i added country list in DropDownList2. I went through virtual and physical path and thus through server.mappath function. I made document folder inside my website to place country list Document.txt and gave reference using virtual path method.Here is my modified code for my application inside page_load method.:
 
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
{
DropDownList2.Items.Clear();
String add = Server.MapPath("~/document/countries.txt");
String conStr = File.ReadAllText(add);
String[] conlist = conStr.Split('\n');
foreach (var contry in conlist)
{
DropDownList2.Items.Add(contry);
}
}

Comments