How To: Create A File - Visual Basic | Microsoft Learn
Maybe your like
Share via
Facebook x.com LinkedIn Email Copy Markdown PrintNote
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
How to: Create a File in Visual Basic Feedback Summarize this article for meIn this article
This example creates an empty text file at the specified path using the Create method in the File class.
Example
Imports System.IO Imports System.Text Module Module1 Sub Main() Dim path As String = "c:\temp\MyTest.txt" ' Create or overwrite the file. Dim fs As FileStream = File.Create(path) ' Add text to the file. Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.") fs.Write(info, 0, info.Length) fs.Close() End Sub End ModuleCompiling 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:
The path name is malformed. For example, it contains illegal characters or is only white space (ArgumentException).
The path is read-only (IOException).
The path name is Nothing (ArgumentNullException).
The path name is too long (PathTooLongException).
The path is invalid (DirectoryNotFoundException).
The path is only a colon ":" (NotSupportedException).
.NET Framework 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
- System.IO
- Create
.NET
Open a documentation issue Provide product feedbackFeedback
Was this page helpful?
Yes No NoNeed help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Ask Learn Ask Learn Suggest a fix?Additional resources
- Last updated on 2022-05-26
In this article
Was this page helpful?
Yes No NoNeed help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Ask Learn Ask Learn Suggest a fix?Tag » How To Create A Text File
-
How To Quickly Create A New, Blank Text File On Windows, Mac, And ...
-
How To Create A Text File - Computer Hope
-
How To Make A Text File
-
Create A Text File And Save It As Text.txt On Your Desktop. - YouTube
-
How To Create A Text File On Windows 10 (Create, Edit & Save)
-
Creating A TXT File - Knowledge Base - DeBounce
-
What Is Text File And How To Create A Text File On Windows
-
How To Create A New Text File In Python
-
How To Create A Plain Text File In Google Drive (and Why You Might ...
-
How To Create (Write) Text File In Python - Guru99
-
Java Create And Write To Files - W3Schools
-
How To Create ".txt" File On Windows | DeviceTests
-
How To Create A Text File In Windows - Bonkers About Tech
-
Ubuntu Linux Create A Text File Using Cat Command - NixCraft