方法: ファイルにテキストを書き込む (Visual Basic)How to: Write Text to Files in Visual Basic
WriteAllText メソッドを利用し、テキストをファイルに書き込みます。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
ファイルにテキストを書き込むには
WriteAllText
メソッドを使い、ファイルと書き込むテキストを指定します。Use theWriteAllText
method to write text to a file, specifying the file and text to be written. この例では、test.txt
という名前のファイルに既に存在するテキストの最後に、"This is new text."
という行を書き込んで追加します。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.
WriteAllText
メソッドを使って、テキストをファイルに書き込みます。書き込むファイルと追加する文字列を指定し、append
をTrue
に設定します。Use theWriteAllText
method to write text to a file, specifying the target file and string to be added and settingappend
toTrue
.この例では、
Documents and Settings
ディレクトリにあるファイルの名前をFileList.txt
に書き込み、読みやすくするために各ファイル名の間に改行を挿入します。This example writes the names of the files in theDocuments and Settings
directory 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:
パスが正しくない。長さが 0 の文字列である、空白だけが含まれている、使用できない文字が含まれている、デバイス パスである (先頭が \\.\)、のいずれかの理由が考えられる (ArgumentException)。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).
パスが
Nothing
であるため、有効でない (ArgumentNullException)The path is not valid because it isNothing
(ArgumentNullException).File
が、存在しないパスを指している (FileNotFoundException または DirectoryNotFoundException)。File
points to a path that does not exist (FileNotFoundException or DirectoryNotFoundException).他のプロセスがファイルを使用しているか、または I/O エラーが発生した (IOException)。The file is in use by another process, or an I/O error occurs (IOException).
パスがシステムで定義されている最大長を超えている (PathTooLongException)。The path exceeds the system-defined maximum length (PathTooLongException).
パス内のファイル名またはディレクトリ名にコロン (:) が含まれているか、または形式が無効である (NotSupportedException)A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
ユーザーがパスを参照するのに必要なアクセス許可がない (SecurityException)The user lacks necessary permissions to view the path (SecurityException).
ディスクがいっぱいになっており、
WriteAllText
の呼び出しが失敗する (IOException)。The disk is full, and the call toWriteAllText
fails (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.