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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Posted Oct 29, 2009, 12:02 PM
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...
Posted Oct 29, 2009, 2:45 PM
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....
Kirtan PatelPosted Oct 29, 2009, 1:32 PM
the code provided by me is for windows Application ..
check "Do you like this answer" if it helps you :)
Arun KumarPosted Oct 29, 2009, 1:22 PM
the above code in console just worked fine..
as i am beginner can u explain it in windows application...
Danatas GerviPosted Oct 29, 2009, 11:34 AM
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?
Kirtan PatelPosted Oct 29, 2009, 11:19 AM
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" :)