热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

文件。C#中的AppendAllText(字符串、字符串、编码)方法,示例

文件。C#中的AppendAllText(字符串、字符串、编码)方法,示例

文件。C# 中的 AppendAllText(字符串、字符串、编码)方法,示例

原文:https://www . geesforgeks . org/file-appendalltextstring-string-encoding-method-in-csharp-with-examples/

文件。AppendAllText(String,String,Encoding) 是一个内置的 File 类方法,用于使用指定的编码将指定的字符串追加到给定的文件中,如果该文件存在,则创建一个新文件,然后追加完成。

语法:

public static void AppendAllText (string path, string contents, System.Text.Encoding encoding);

参数:该函数接受两个参数,如下图所示:



  • Path: This is the file to be appended with the given content.

  • Directory: This is the specified content to be added to the file.

  • Code: This is the specified character code.


例外:


  • ArgumentException: 路径是零长度字符串,只包含空格,或者包含一个或多个由 InvalidPathChars 定义的无效字符。

  • ArgumentNullException: 路径为空。

  • 路径工具异常:给定的路径、文件名或两者都超过了系统定义的最大长度。

  • directory ynotfoundexception:指定的路径无效(例如,目录不存在或位于未映射的驱动器上)。

  • IOException: 打开文件时出现输入/输出错误。

  • 未授权访问异常:路径指定了一个只读文件。或者当前平台不支持此操作。或者路径指定了一个目录。或者呼叫者没有所需的权限。

  • notSupportDexception:路径的格式无效。

  • 安全性异常:调用方没有所需的权限。

下面是说明文件的程序。方法。
程序 1: 在运行下面的代码之前,创建了一个文件,其内容如下所示:

file.txt

c sharp . c sharp . c sharp . c sharp


// C# program to illustrate the usage
// of File.AppendAllText() method
// Using System, System.IO,
// System.Text and System.Linq namespaces
using System;
using System.IO;
using System.Text;
using System.Linq;
class GFG {
    // Main() method
    public static void Main()
    {
        // Creating a file
        string myfile = @"file.txt";
        // Adding extra texts
        string appendText = " is a CS portal." + Environment.NewLine;
        File.AppendAllText(myfile, appendText, Encoding.UTF8);
        // Opening the file to read from.
        string readText = File.ReadAllText(myfile);
        Console.WriteLine(readText);
    }
}

执行:

mcs -out:main.exe main.cs
mono main.exe
GeeksforGeeks is a CS portal.

运行上述代码后,显示上述输出,文件内容如下所示:

file.txt

程序 2: 最初没有创建文件,但是下面的代码本身创建了一个新文件并附加了指定的内容。

c sharp . c sharp . c sharp . c sharp


// C# program to illustrate the usage
// of File.AppendAllText() method
// Using System, System.IO,
// System.Text and System.Linq namespaces
using System;
using System.IO;
using System.Text;
using System.Linq;
class GFG {
    // Main() method
    public static void Main()
    {
        // Creating a file
        string myfile = @"file.txt";
        // Checking the existence of file
        if (!File.Exists(myfile)) {
            // Creating a file with below content
            string createText = "GFG" + Environment.NewLine;
            File.WriteAllText(myfile, createText);
        }
        // Adding extra texts
        string appendText = " is a CS portal." + Environment.NewLine;
        File.AppendAllText(myfile, appendText, Encoding.UTF8);
        // Opening the file to read from.
        string readText = File.ReadAllText(myfile);
        Console.WriteLine(readText);
    }
}

执行:

mcs -out:main.exe main.cs
mono main.exe
GFG
is a CS portal.

运行上述代码后,显示了上述输出,并创建了一个新文件,如下所示:

file.txt


推荐阅读
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • Java 中 Writer flush()方法,示例 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 探讨如何通过编程技术实现100个并发连接,解决线程创建顺序问题,并提供高效的并发测试方案。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本文介绍了如何使用 Spring Boot DevTools 实现应用程序在开发过程中自动重启。这一特性显著提高了开发效率,特别是在集成开发环境(IDE)中工作时,能够提供快速的反馈循环。默认情况下,DevTools 会监控类路径上的文件变化,并根据需要触发应用重启。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文介绍如何利用动态规划算法解决经典的0-1背包问题。通过具体实例和代码实现,详细解释了在给定容量的背包中选择若干物品以最大化总价值的过程。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
author-avatar
你是我的小心肝啊哈哈
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有