.net读取写入文本操作
来源:互联网 发布:为知笔记免登陆 编辑:程序博客网 时间:2024/06/17 05:51
string creatfile = Server.MapPath(@"~/datasource.xml");
//写入之前清空文本内容
System.IO.File.ReadAllText(creatfile, Encoding.GetEncoding("gb2312"));
FileStream stream = System.IO.File.Create(creatfile);
stream.Close();
using (System.IO.StreamWriter tempsw = new System.IO.StreamWriter(creatfile, true, Encoding.GetEncoding("gb2312")))
{
tempsw.WriteLine(strxml);//写入最新数据内容
tempsw.Flush();
tempsw.Close();
}
其他操作整理:
c#创建文本
private const string FILE_NAME = "ErroLog.txt";
public static void WriteFile(string str)
...{
StreamWriter sr;
if (File.Exists(FILE_NAME)) //如果文件存在,则创建File.AppendText对象
...{
sr = File.AppendText(FILE_NAME);
}
else //如果文件不存在,则创建File.CreateText对象
...{
sr = File.CreateText(FILE_NAME);
}
sr.WriteLine(str);
sr.Close();
}
C#追加文件
StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt");
sw.WriteLine("追逐理想");
sw.WriteLine("kzlll");
sw.WriteLine(".NET笔记");
sw.Flush();
sw.Close();
C#拷贝文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\\myText.txt";
NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Copy(OrignFile,NewFile,true);
C#删除文件
string delFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Delete(delFile);
C#移动文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\\myText.txt";
NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Move(OrignFile,NewFile);
C#创建目录
// 创建目录c:\sixAge
DirectoryInfo d=Directory.CreateDirectory("c:\\sixAge");
// d1指向c:\sixAge\sixAge1
DirectoryInfo d1=d.CreateSubdirectory("sixAge1");
// d2指向c:\sixAge\sixAge1\sixAge1_1
DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");
// 将当前目录设为c:\sixAge
Directory.SetCurrentDirectory("c:\\sixAge");
// 创建目录c:\sixAge\sixAge2
Directory.CreateDirectory("sixAge2");
// 创建目录c:\sixAge\sixAge2\sixAge2_1
Directory.CreateDirectory("sixAge2\\sixAge2_1");
递归删除文件夹及文件
<%@ Page Language=C#%>
<%@ Import namespace="System.IO"%>
<Script runat=server>
public void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach(string d in Directory.GetFileSystemEntries(dir))
{
if(File.Exists(d))
File.Delete(d); //直接删除其中的文件
else
DeleteFolder(d); //递归删除子文件夹
}
Directory.Delete(dir); //删除已空文件夹
Response.Write(dir+" 文件夹删除成功");
}
else
Response.Write(dir+" 该文件夹不存在"); //如果文件夹不存在则提示
}
protected void Page_Load (Object sender ,EventArgs e)
{
string Dir="D:\\gbook\\11";
DeleteFolder(Dir); //调用函数删除文件夹
}
- .net读取写入文本操作
- VB.net 文件读取、写入、追加操作
- 文本写入读取
- java文本读取写入
- asp.net创建并写入文本和读取文件内容
- asp.net cookie的操作,写入、读取与操作
- asp.net cookie的操作,写入、读取与操作
- java逐行写入读取文本
- ASP.NET cookie 操作详解cookie 写入、读取、修改、删除
- 转贴:ASP.NET- Cookie操作详解(写入、读取、修改)
- ASP.NET cookie 操作详解cookie 写入、读取、修改、删除
- ASP.NET cookie 操作详解|cookie 写入、读取、修改、删除
- .net XML 读取 写入
- Perl读取文本格式化后写入文本
- asp.net文件读取 写入
- PHP大数据文本读取与写入
- c++ 搜索读取文本另写入
- getc(),putc()文件文本读取写入函数
- c语言中的类型限定词之一restrict
- 数据库注意知识点
- ios 用个人签名的应用不能同时发布企业 Distribution
- 严重: IOException while loading persisted sessions: java.io.EOFException
- Ubuntu 11.04 32位系统下 SRILM 的配置详解
- .net读取写入文本操作
- css中有些属性的前面会加上“*”“_”,请问分别表示什么意思?
- 递归字符串翻转"abc"->"cba"
- SAC 安装的问题
- paip.URL参数压缩64进制
- Hibernate for循環save() 問題
- Java程序员不该有的6种陋习
- Linux查看系统语系(编码)命令
- 如何通过超级终端 利用串口实现arm板子和pc互相发送文件???