I am new to c# coding community. All i want to know is how to write and read a simple text file inwindows apllications
With Regards
Arun Kumar
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Posted Oct 29, 2009, 8:02 AM EDT
Say u have a text file called hello.txt in c:now i'll teach u how to read text from hello.txt and print it....
using System;
using System.Collections.Generic;
using System.text;
using System.IO;
namespace textfile
{
class program
{
static void Main(string [] args)
{
StreamReader sr=new Streamreader("C:\\hello.txt");
Console.WriteLine(sr.Readline());
sr.close();
}
}
}
Same if u want to create a new a textfile and write something in it..The following code explain
using System;
using System.Collections.Generic;
using System.text;
using System.IO;
namespace textfile
{
class program
{
static void Main(string [] args)
{
StreamWriter sw=new StreamWriter("C:\\writenew.txt");
sw.WriteLine("trying to write something in the text file");
sw.close();
}
}
}
Hoped it help..Any doubts feel free to ask....
If Someway u Liked this answer don't forget to mark it as correct answer...
Kirtan PatelRank #292 · 8.6K reputationPosted Oct 29, 2009, 7:19 AM EDT
Here is Simple Code to Read and Write Text File
add below line at Top Of the Code
using System.IO;
and Write Code in Button Like Below
private void btnWrite_Click(object sender, EventArgs e)
{
//Writing Data into TextFile
string DataYouWantoToWrite = textBox1.Text;
StreamWriter sw = new StreamWriter("sample.txt");
sw.Write(DataYouWantoToWrite);
sw.Close();
}
private void btnRead_Click(object sender, EventArgs e)
{
//Now Read The Data That We have Written in to Text BOx
string ReadData = File.ReadAllText("sample.txt");
textBox1.Text = ReadData;
}
If it Helps you please check "Do you like this answer" :)
Danatas GerviRank #4,152 · 361 reputationPosted Oct 29, 2009, 7:34 AM EDT
May be this also possible?
private void btnWrite_Click(object sender, EventArgs e)
{
string DataYouWantoToWrite = textBox1.Text;
File.AppendAllText("sample.txt", DataYouWantoToWrite);
}
To Kirtan – Why do you prefer to use the StreamWriter?
Arun Kumar5 reputationPosted Oct 29, 2009, 9:22 AM EDT
the above code in console just worked fine..
as i am beginner can u explain it in windows application...
Kirtan PatelRank #292 · 8.6K reputationPosted Oct 29, 2009, 9:32 AM EDT
the code provided by me is for windows Application ..
check "Do you like this answer" if it helps you :)
Posted Oct 29, 2009, 10:45 AM EDT
First create a new windows application.Hope u know it.Then drag a textbox and two command buttons.name the buttons as Read and write.
double click the Write cmd button and add the following code
private void Write_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter("C:\\writenew.txt");
string str = textBox1.Text;
sw.WriteLine(str);
MessageBox.Show("successfully witten");
sw.Close();
}
This writes data given in textbox into writenew.txt file which gets stored in c: drive
Next step is to read all the data from a text file and display it in the textbox.For this doubleclick the Read command button and add the following code..
private void Read_Click(object sender, EventArgs e)
{
string ReadData = File.ReadAllText("c:\\writenew.txt");
textBox1.Text = ReadData;
}
Hope this help feel free to ask any doubts....