The Open method opens a FileStream on the specified file in the specified file mode.
FileStream fs = fi.Open(FileMode.Open, FileAccess.Write);
Here is the complete code sample.
string fileName = @"C:fileNametxt";
FileInfo fi = new FileInfo(fileName);
// If file does not exist, create file
if (!fi.Exists)
{
//Create the file.
using (FileStream fs = fi.Create())
{
Byte[] info = new UTF8Encoding(true).GetBytes("File Start");
fs.Write(info, 0, info.Length);
}
}
try
{
using (FileStream fs = fi.Open(FileMode.Open, FileAccess.Write))
{
Byte[] info = new UTF8Encoding(true).GetBytes("Add more text");
fs.Write(info, 0, info.Length);
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
NOTE:
A File must be opened using an IO resource before it can be read or write to. A file can be opened to read and/or write purpose. The FileInfo class provides four methods to open a file.
+ Open
+ OpenRead
+ OpenText
+ OpenWrite