We can save data in database, xml format,text format and some other places.Here we are saving our data in Text Format(In notepad).It's a very important chapter..Please try it.
on which event we want to perform create text file,write following code.....
using system.text;
using system.io;
TextWriter tw = new StreamWriter("C:\\Documents and Settings\\pardeep\\Desktop\\sonu.txt");
tw.WriteLine(DateTime.Now);
tw.Close();
TextReader tr = new StreamReader("C:\\Documents and Settings\\pardeep\\Desktop\\date.txt");
Console.WriteLine(tr.ReadLine());
Console.ReadLine();
tr.Close();
Int64 x;
StreamWriter sw = new StreamWriter("C:\\Documents and Settings\\pardeep\\Desktop\\sample.txt",true,Encoding.ASCII);
for (x = 0; x < 10; x++)
{
sw.WriteLine(x);
Console.ReadLine();
}
sw.Close();
string line;
StreamReader sr = new StreamReader("C:\\Documents and Settings\\pardeep\\Desktop\\sample.txt");
line=sr.ReadLine();
{
Console.WriteLine(line);
line=sr.ReadLine();
}
sr.Close();
sw.WriteLine("Welcome");
sw.WriteLine("Hi");
name textbox1
age textbox2
savebutton showbutton
protected void savebutton_Click(object sender, EventArgs e)
{
string data = TextBox1.Text;
string data2 = TextBox2.Text;
sw.WriteLine("Name="+ data);
sw.WriteLine("Age="+data2);
}
string data3 = data+data2;
File.WriteAllText("C:\\Documents and Settings\\pardeep\\Desktop\\sample.txt",data);
protected void Btn_textshow_Click(object sender, EventArgs e)
{
string contents = File.ReadAllText(path);
Label2.Text = contents;
Label2.Text = contents.Replace("\r\n", "<br />");
}
{
At first,we see how to create text file.......
on which event we want to perform create text file,write following code.....
using system.text;
using system.io;
//above namespace will write first.It is necessary for any type of text write or read in text file.//in streamwriter under "is written path where we want to create file and name of that file"
TextWriter tw = new StreamWriter("C:\\Documents and Settings\\pardeep\\Desktop\\sonu.txt");
tw.WriteLine(DateTime.Now);
tw.Close();
//below code is used for read the file address given in streamreader
TextReader tr = new StreamReader("C:\\Documents and Settings\\pardeep\\Desktop\\date.txt");
Console.WriteLine(tr.ReadLine());
Console.ReadLine();
tr.Close();
//Now, below code helps you to write in a text file from 1 to 10
Int64 x;
StreamWriter sw = new StreamWriter("C:\\Documents and Settings\\pardeep\\Desktop\\sample.txt",true,Encoding.ASCII);
for (x = 0; x < 10; x++)
{
sw.WriteLine(x);
Console.ReadLine();
}
sw.Close();
//true shows that previous written in your current file will available. but if you write false the place //of true then the data previously written in that file is removed from there.
//for read data from any notepad file where data is already written
string line;
StreamReader sr = new StreamReader("C:\\Documents and Settings\\pardeep\\Desktop\\sample.txt");
line=sr.ReadLine();
while(line!=null)
{
Console.WriteLine(line);
line=sr.ReadLine();
}
sr.Close();
// for write data in any notepad file which is created earlier
StreamWriter sw = new StreamWriter("C:\\Documents and Settings\\pardeep\\Desktop\\sample.txt");
sw.WriteLine("Welcome");
sw.WriteLine("Hi");
sw.Close();
Console.ReadLine();
//now save data in text format(in notepad file) from textbox in our web page....
//our form wiill look like foolowing ..
name textbox1
age textbox2
savebutton showbutton
now, see code on click of these two buttons...
protected void savebutton_Click(object sender, EventArgs e)
{
string data = TextBox1.Text;
string data2 = TextBox2.Text;
StreamWriter sw =new StreamWriter(path,true,Encoding.ASCII);
sw.WriteLine("Name="+ data);
sw.WriteLine("Age="+data2);
sw.Close();
}
or we can write these codes behind of above code...both will work same
string data3 = data+data2;
File.WriteAllText("C:\\Documents and Settings\\pardeep\\Desktop\\sample.txt",data);
in showbutton we want to show that data in our page in label or any where
protected void Btn_textshow_Click(object sender, EventArgs e)
{
string contents = File.ReadAllText(path);
Label2.Text = contents;
Label2.Text = contents.Replace("\r\n", "<br />");
}
or we can write place of above code
{
string content = File.ReadAllText("C:\\Documents and Settings\\pardeep\\Desktop\\sample.txt");
Label1.Text = content.Replace("\r\n","<br/>");
Label1.Text = content.Replace("\r\n","<br/>");
}
you can check above code in asp.net...and reply me if there is any problem regarding this.
Thanks
Amitesh Anand
Software Developer
you can check above code in asp.net...and reply me if there is any problem regarding this.
Thanks
Amitesh Anand
Software Developer
Set text format for label in C#.NET
ReplyDelete