To avoid accidentally overwriting an existing file use:
using (new FileStream(filename, FileMode.CreateNew)) {}
...and handle the IOException which will occur if the file already exists.
File.Create
, which is suggested in other answers, will overwrite the contents of the file if it already exists. In simple cases you could mitigate this using File.Exists()
. However something more robust is necessary in scenarios where multiple threads and/or processes are attempting to create files in the same folder simultaneously.