c# -- 介绍File.AppendAllText 方法
下面介绍两个函数:
File.AppendAllText (String, String)
File.AppendAllText (String, String, String)
File.AppendAllText 方法 (String, String)
函数说明:打开一个文件,向其中追加指定的字符串,然后关闭该文件。 如果文件不存在,此方法创建一个文件,将指定的字符串写入文件,然后关闭该文件。
命名空间: System.IO
程序集: mscorlib(在 mscorlib.dll 中)
语法:
public static void AppendAllText(
string path,
string contents
)
参数说明:
path
类型:System.String,要将指定的字符串追加到的文件。
contents
- 类型:System.String, 要追加到文件中的字符串。
代码示例:
下面的代码示例演示如何使用 AppendAllText 方法将额外的文本添加到文件末尾。 在此示例中,该文件后,如果它已不存在,并且,文本添加到。
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);
}
}
- 注意点:
已知字符串和文件路径,此方法打开指定的文件,将字符串追加到文件末尾,然后关闭文件。 即使会引发异常,也使用此方法保证文件句柄已关闭。
方法创建文件,如果不存在,则,但不创建新目录。 因此,path 参数的值必须包含现有内容。
File.AppendAllText(String, String, Encoding)
函数说明:将指定的字符串追加到文件中,如果文件还不存在则创建该文件。
命名空间: System.IO
程序集: mscorlib(在 mscorlib.dll 中)
语法:
public static void AppendAllText(
string path,
string contents,
Encoding encoding
)
参数说明:
- path
- 类型:System.String,要将指定的字符串追加到的文件。
contents
类型:System.String,要追加到文件中的字符串。
encoding
类型:System.Text.Encoding,要使用的字符编码。
用法:
File.AppendAllText(path, contents, Encoding)
如:File.AppendAllText(path, appendText, Encoding.UTF8);
- 附录:异常表
异常 | 条件 |
---|---|
ArgumentException |
path 是一个零长度字符串,仅包含空白或者包含一个或多个由 InvalidPathChars 定义的无效字符。 |
ArgumentNullException |
path 为 null。 |
PathTooLongException |
指定的路径、文件名或者两者都超出了系统定义的最大长度。 例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。 |
DirectoryNotFoundException |
指定路径无效(例如,目录不存在或位于未映射的驱动器上)。 |
IOException |
打开文件时发生 I/O 错误。 |
UnauthorizedAccessException |
path 指定了一个只读文件。 - 或 - 在当前平台上不支持此操作。 - 或 - path 指定了一个目录。 - 或 - 调用方没有所要求的权限。 |
FileNotFoundException |
未找到 path 中指定的文件。 |
NotSupportedException |
path 的格式无效。 |
SecurityException |
调用方没有所要求的权限。 |
c# -- 介绍File.AppendAllText 方法的更多相关文章
- react-native热更新之CodePush详细介绍及使用方法
react-native热更新之CodePush详细介绍及使用方法 2018年03月04日 17:03:21 clf_programing 阅读数:7979 标签: react native热更新co ...
- block(代码块)的介绍以及使用方法和变量之间的关系
http://blog.csdn.net/menxu_work/article/details/8762848 block(代码块)的介绍以及使用方法和变量之间的关系 block(代码块)的介绍以及使 ...
- Python学习(16)File(文件)方法
Python File(文件) 方法 file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读 ...
- vss error reading from file 解决方法
vss error reading from file 解决方法 1 若服务器中存在 vss/data/backup目录,请将该目录删掉2 运行cmd cd.. cd C:\Program Files ...
- 【转】【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法
原始地址:http://blog.csdn.net/yangyu20121224/article/category/1431917/1 由于TabActivity在Android4.0以后已经被完全弃 ...
- centos vsftpd 553 Could not create file解决方法
centos vsftpd 553 Could not create file解决方法 问题由于selinux引起的,问题解决办法: www.2cto.com 输入:getsebool - ...
- C#中当程序的访问权限不足时,Directory.Exists和File.Exists方法不会抛出异常报错
有些时候,我们开发的C#应用程序的执行账号,可能没有对一些文件夹和文件的访问权限,当我们使用Directory.Exists和File.Exists方法去判断这些文件夹和文件是否存在的时候,Direc ...
- file.seek()方法引出的文本文件和二进制文件问题
问题的起因 菜鸟教程上有一段关于file.seek()方法的讲解,先简短描述一下seek()方法: seek(offset, whence)方法用于移动文件读取指针到指定位置 参数offset--开始 ...
- 【翻译自mos文章】使用asmcmd命令在本地和远程 asm 实例之间 拷贝asm file的方法
使用asmcmd命令在本地和远程 asm 实例之间 拷贝asm file的方法 參考原文: How to Copy asm files between remote ASM instances usi ...
随机推荐
- webgote的例子(2)Sql注入(SearchGET)
Sql注入(Search/GET) 大家好!!! 现如今web服务在我们的网络上遍地都是,各个终端设备成为我们看不见的客户,web服务也成为公司的招牌.80 443为我们展现的视角也是多姿多彩但背后新 ...
- Ubuntu 17.10 用 apt 搭建 lamp 环境(精简版)
这篇文章主要用来快速部署以 php 5.6 为主的 lamp 环境,要看详细安装包括虚拟主机配置的请参考这篇:http://www.cnblogs.com/mingc/p/7864030.html 一 ...
- atoll()函数使用注意事项及分析
atoll是c99标准加入的函数,在编译的时候可能要打开C99标准的编译选项 -std=c99. 另外,必须包含stdlib.h头文件,否则会出错. ☞ C程序代码如下所示: #include < ...
- 控制终端tcgetattr函数与tcsetattr函数
tcgetattr(fd,&oldios); //获得与终端相关的参数,参数保存在oldios中 newios.c_cflag = nSpeed | CS8 | CLOCAL | CREAD; ...
- oracle 一个网站
http://www.oracle.com/technetwork/cn/articles/11g-pivot-101924-zhs.html
- Appium+python 一个简单的登录测试实例
# coding=utf-8 from appium import webdriver import time import unittest import os import HTMLTestRun ...
- csu 1329 一行盒子(链表操作)
1329: 一行盒子 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 693 Solved: 134 [Submit][Status][Web Boa ...
- 得分(UVa1585)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- 当参数为带参数的url时怎么办?
比如地址为:http://www.baidu.com/index.aspx?url=http://www.baidu.com/info.aspx?id=1&type=1,用Request[&q ...
- Python全栈开发之7、模块和几种常见模块以及format知识补充
一.模块的分类 Python流行的一个原因就是因为它的第三方模块数量巨大,我们编写代码不必从零开始重新造轮子,许多要用的功能都已经写好封装成库了,我们只要直接调用即可,模块分为内建模块.自定义的模块. ...