************************read text file*************************
Dim fp As StreamReader
Try
fp = File.OpenText(Server.MapPath(".\Upload\") & "test.txt")
txtMyFile.Text = fp.ReadToEnd()
lblStatus.Text = "File Succesfully Read!"
fp.Close()
*********************create text file*****************************
Dim fp As StreamWriter
Try
fp = File.CreateText(Server.MapPath(".\Upload\") & "test.txt")
fp.WriteLine(txtMyFile.Text)
lblStatus.Text = "File Succesfully created!"
fp.Close()
Catch err As Exception
lblStatus.Text = "File Creation failed. Reason is as follows
************************write doc file*******************************
private void WriteToWordFile()
{
// Any folder
string path = @"C:\ServerFolder\MyWordFile.doc";
string text = TextBox2.Text;
// Put it in try-catch finally :)
FileStream fs = File.Create(path);
fs.Close();
StreamWriter sw = new StreamWriter(path);
sw.Write(text);
sw.Close();
}
****************************write text into file*****************************
Writing to a Text File
Listing 1: Writing Text Data to a File: TextFileWriter.cs
using System;
using System.IO;
namespace csharp_station.howto
{
class TextFileWriter
{
static void Main(string[] args)
{
// create a writer and open the file
TextWriter tw = new StreamWriter("date.txt");
// write a line of text to the file
tw.WriteLine(DateTime.Now);
// close the stream
tw.Close();
}
}
}
****************************read text from file*****************************
using System;
using System.IO;
namespace csharp_station.howto
{
class TextFileReader
{
static void Main(string[] args)
{
// create reader & open file
Textreader tr = new StreamReader("date.txt");
// read a line of text
Console.WriteLine(tr.ReadLine());
// close the stream
tr.Close();
}
}
}
No comments:
Post a Comment