Using the FileStream Namespace to create a text file, then write a string to it.
Namespace required:
using System.IO;
The code:
protected void Writetofile()
{
FileStream fs;
char[] array;
try
{
fs = new FileStream("test.txt", FileMode.Create); // create a text file in the application directory
String str = "Welcome to the Scripts Library";
array = str.ToCharArray ();
for (int i = 0; i < array.GetLength(0); i++)
fs.WriteByte ((byte) char.Parse(array.GetValue(i).ToString()));
fs.Close();
MessageBox.Show("File was created!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Run this code, then check you application directory for the test.txt file.
Open the file, what did you find in it?
Happy Coding!




