How to: Create a File in Visual Basic

This example creates an empty text file at the specified path using the Create method in the File class.

Example

Dim file As System.IO.FileStream
file = System.IO.File.Create("c:\test.txt")

Compiling the Code

Use the file variable to write to the file.

Robust Programming

If the file already exists, it is replaced.

The following conditions may cause an exception:

Security

A SecurityException may be thrown in partial-trust environments.

The call to the Create method requires FileIOPermission.

An UnauthorizedAccessException is thrown if the user does not have permission to create the file.

See Also

Concepts

Using Libraries from Partially Trusted Code

Code Access Security Basics

Reference

System.IO

Create