System.IO.File.AppendAllText C# (CSharp) Méthode

AppendAllText() public static méthode

public static AppendAllText ( String path, String contents ) : void
path String
contents String
Résultat void
        public static void AppendAllText(String path, String contents)
        {
            if (path == null)
                throw new ArgumentNullException(nameof(path));
            if (path.Length == 0)
                throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
            Contract.EndContractBlock();

            using (StreamWriter sw = new StreamWriter(path, append: true))
            {
                sw.Write(contents);
            }
        }

Same methods

File::AppendAllText ( String path, String contents, Encoding encoding ) : void
File::AppendAllText ( string path, string contents ) : void
File::AppendAllText ( string path, string contents, System encoding ) : void

Usage Example

    /// <summary>
    ///     Appends text to a specified file path.
    /// </summary>
    /// <param name="path">The path of the file.</param>
    /// <param name="contents">The contents.</param>
    /// <param name="encoding">The encoding to use.</param>
    /// <exception cref="ArgumentNullException">
    ///     <paramref name="path" /> or <paramref name="contents" /> is <see langword="null" /> (<see langword="Nothing" /> in
    ///     Visual Basic).
    /// </exception>
    /// <remarks>
    ///     This operation always requires an encoding to be used. If <paramref name="encoding" /> is set to
    ///     <see langword="null" />, an implementation-specific
    ///     encoding will be used.
    /// </remarks>
    public void AppendAllText(
        string path,
        string contents,
        Encoding?encoding = null)
    {
        _ = Requires.NotNullOrWhiteSpace(
            path,
            nameof(path));
        _ = Requires.NotNullOrWhiteSpace(
            contents,
            nameof(contents));

        if (encoding == null)
        {
            FSFile.AppendAllText(
                path,
                contents);
        }
        else
        {
            FSFile.AppendAllText(
                path,
                contents,
                encoding);
        }
    }
All Usage Examples Of System.IO.File::AppendAllText