Nóta
Teastaíonn údarú chun rochtain a fháil ar an leathanach seo. Is féidir leat triail a bhaint as shíniú isteach nó eolairí a athrú.
Teastaíonn údarú chun rochtain a fháil ar an leathanach seo. Is féidir leat triail a bhaint as eolairí a athrú.
The WriteAllText method can be used to write text to files. If the specified file does not exist, it is created.
Procedure
To write text to a file
Use the
WriteAllTextmethod to write text to a file, specifying the file and text to be written. This example writes the line"This is new text."to the file namedtest.txt, appending the text to any existing text in the file.My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt", "This is new text to be added.", True)
To write a series of strings to a file
Loop through the string collection. Use the
WriteAllTextmethod to write text to a file, specifying the target file and string to be added and settingappendtoTrue.This example writes the names of the files in the
Documents and Settingsdirectory toFileList.txt, inserting a carriage return between each for better readability.For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\Documents and Settings") foundFile = foundFile & vbCrLf My.Computer.FileSystem.WriteAllText( "C:\Documents and Settings\FileList.txt", foundFile, True) Next
Robust Programming
The following conditions may cause an exception:
The path is not valid for one of the following reasons: it is a zero-length string, it contains only white space, it contains invalid characters, or it is a device path (starts with \\.\) (ArgumentException).
The path is not valid because it is
Nothing(ArgumentNullException).Filepoints to a path that does not exist (FileNotFoundException or DirectoryNotFoundException).The file is in use by another process, or an I/O error occurs (IOException).
The path exceeds the system-defined maximum length (PathTooLongException).
A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
The user lacks necessary permissions to view the path (SecurityException).
The disk is full, and the call to
WriteAllTextfails (IOException).
If you are running in a partial-trust context, the code might throw an exception due to insufficient privileges. For more information, see Code Access Security Basics.