C# File Open(String, FileMode)
Description
File Open(String, FileMode)
Opens a FileStream on the
specified path with read/write access.
Syntax
File.Open(String, FileMode)
has the following syntax.
public static FileStream Open(
string path,
FileMode mode
)
Parameters
File.Open(String, FileMode)
has the following parameters.
path
- The file to open.mode
- A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.
Returns
File.Open(String, FileMode)
method returns A FileStream opened in the specified mode and path, with read/write access
and not shared.
Example
The following code example creates a temporary file and writes some text to it.
/*ww w .ja v a2 s .c o m*/
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = Path.GetTempFileName();
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None))
{
Byte[] info = new UTF8Encoding(true).GetBytes("from java2s.com");
fs.Write(info, 0, info.Length);
}
using (FileStream fs = File.Open(path, FileMode.Open))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}
The code above generates the following result.