File.WriteAllText メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
新しいファイルを作成し、内容をそのファイルに書き込んだ後、ファイルを閉じます。 既存のターゲット ファイルは上書きされます。
オーバーロード
WriteAllText(String, String) |
新しいファイルを作成し、指定した文字列をそのファイルに書き込んだ後、ファイルを閉じます。 既存のターゲット ファイルは上書きされます。 |
WriteAllText(String, String, Encoding) |
新しいファイルを作成し、指定したエンコーディングで指定の文字列をそのファイルに書き込んだ後、ファイルを閉じます。 既存のターゲット ファイルは上書きされます。 |
WriteAllText(String, String)
新しいファイルを作成し、指定した文字列をそのファイルに書き込んだ後、ファイルを閉じます。 既存のターゲット ファイルは上書きされます。
public:
static void WriteAllText(System::String ^ path, System::String ^ contents);
public static void WriteAllText (string path, string contents);
public static void WriteAllText (string path, string? contents);
static member WriteAllText : string * string -> unit
Public Shared Sub WriteAllText (path As String, contents As String)
パラメーター
- path
- String
書き込み先のファイル。
- contents
- String
ファイルに書き込む文字列。
例外
.NET Framework バージョンと .NET Core バージョンが 2.1 より前の場合: path
は長さ 0 の文字列、空白のみを含む、または無効な文字が 1 つ以上含まれています。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。
path
が null
です。
指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。
指定されたパスが正しくありません (たとえば、マップされていないドライブにあるなど)。
ファイルを開くときに、I/O エラーが発生しました。
path
が読み取り専用のファイルを指定しました。
- または -
path
によって、非表示のファイルが指定されました。
- または -
この操作は、現在のプラットフォームではサポートされていません。
- または -
path
がディレクトリを指定しました。
- または -
呼び出し元に、必要なアクセス許可がありません。
path
の形式が正しくありません。
呼び出し元に、必要なアクセス許可がありません。
例
次のコード例では、 メソッドを使用 WriteAllText してファイルにテキストを書き込む方法を示します。 この例では、ファイルがまだ存在しない場合は作成され、テキストが追加されます。
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText);
// Open the file to read from.
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
}
}
open System
open System.IO
let path = @"c:\temp\MyTest.txt"
// This text is added only once to the file.
if File.Exists path |> not then
// Create a file to write to.
let createText =
"Hello and Welcome" + Environment.NewLine
File.WriteAllText(path, createText)
// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
"This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText)
// Open the file to read from.
let readText = File.ReadAllText path
printfn $"{readText}"
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
Dim createText As String = "Hello and Welcome" + Environment.NewLine
File.WriteAllText(path, createText)
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText)
' Open the file to read from.
Dim readText As String = File.ReadAllText(path)
Console.WriteLine(readText)
End Sub
End Class
注釈
このメソッドは、Byte-Order Mark (BOM) を使用せずに UTF-8 エンコードを使用するため、 メソッドを GetPreamble 使用すると空のバイト配列が返されます。 ファイルの先頭にバイト順マークなどの UTF-8 識別子を含める必要がある場合は、 メソッドオーバーロードとUTF8エンコードを使用WriteAllText(String, String, Encoding)します。
文字列とファイル パスを指定すると、このメソッドは指定したファイルを開き、その文字列をファイルに書き込んでからファイルを閉じます。
適用対象
WriteAllText(String, String, Encoding)
新しいファイルを作成し、指定したエンコーディングで指定の文字列をそのファイルに書き込んだ後、ファイルを閉じます。 既存のターゲット ファイルは上書きされます。
public:
static void WriteAllText(System::String ^ path, System::String ^ contents, System::Text::Encoding ^ encoding);
public static void WriteAllText (string path, string contents, System.Text.Encoding encoding);
public static void WriteAllText (string path, string? contents, System.Text.Encoding encoding);
static member WriteAllText : string * string * System.Text.Encoding -> unit
Public Shared Sub WriteAllText (path As String, contents As String, encoding As Encoding)
パラメーター
- path
- String
書き込み先のファイル。
- contents
- String
ファイルに書き込む文字列。
- encoding
- Encoding
文字列に適用するエンコーディング。
例外
.NET Framework バージョンと .NET Core バージョンが 2.1 より前の場合: path
は長さ 0 の文字列、空白のみを含む、または無効な文字が 1 つ以上含まれています。 正しくない文字を照会するには、GetInvalidPathChars() メソッドを使用します。
path
が null
です。
指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。
指定されたパスが正しくありません (たとえば、マップされていないドライブにあるなど)。
ファイルを開くときに、I/O エラーが発生しました。
path
が読み取り専用のファイルを指定しました。
- または -
path
によって、非表示のファイルが指定されました。
- または -
この操作は、現在のプラットフォームではサポートされていません。
- または -
path
がディレクトリを指定しました。
- または -
呼び出し元に、必要なアクセス許可がありません。
path
の形式が正しくありません。
呼び出し元に、必要なアクセス許可がありません。
例
次のコード例では、 メソッドを使用 WriteAllText してファイルにテキストを書き込む方法を示します。 この例では、ファイルがまだ存在しない場合は作成され、テキストが追加されます。
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText, Encoding.UTF8);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText, Encoding.UTF8);
// Open the file to read from.
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
}
}
open System
open System.IO
open System.Text
let path = @"c:\temp\MyTest.txt"
// This text is added only once to the file.
if File.Exists path |> not then
// Create a file to write to.
let createText =
"Hello and Welcome" + Environment.NewLine
File.WriteAllText(path, createText, Encoding.UTF8)
// This text is always added, making the file longer over time
// if it is not deleted.
let appendText =
"This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText, Encoding.UTF8)
// Open the file to read from.
let readText = File.ReadAllText path
printfn $"{readText}"
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim sw As StreamWriter
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
Dim createText As String = "Hello and Welcome" + Environment.NewLine
File.WriteAllText(path, createText, Encoding.UTF8)
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText, Encoding.UTF8)
' Open the file to read from.
Dim readText As String = File.ReadAllText(path)
Console.WriteLine(readText)
End Sub
End Class
注釈
文字列とファイル パスを指定すると、このメソッドは指定したファイルを開き、指定したエンコードを使用して文字列をファイルに書き込み、ファイルを閉じます。 例外が発生した場合でも、ファイル ハンドルはこのメソッドによって閉じられることが保証されます。