.NET中的文件IO操作实例
2014-09-18 09:47
387 查看
1.写入文件代码:
2.读取文件代码:
3. 删除文件:
//1.1 生成文件名和设置文件物理路径 Random random = new Random(DateTime.Now.Millisecond); string fileName = System.DateTime.Now.ToString("yyMMddHHmmss")+random.Next(10000); string PhysicalPath = Server.MapPath("../File/Template/productDetails/" + fileName + ".txt"); //1.2 判断文件是否存在 if (!File.Exists(PhysicalPath)) { FileStream fs = new FileStream(PhysicalPath, FileMode.Create); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8); sw.Write(proDetails); sw.Close(); fs.Close(); } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "addAtt", "<script>alert('添加失败,请重试!');location.href='#'</script>"); }
2.读取文件代码:
//将商品详情写入文件中 string detailsPath=CreateFile(proDetails); string filePath = Server.MapPath("../File/Template/productDetails/" + detailsPath+".txt"); if (File.Exists(url)) { // Create the file. using (FileStream fs=new FileStream(url,FileMode.Open)) { StreamReader sr=new StreamReader(fs,Encoding.Default); this.aaa.InnerHtml = sr.ReadToEnd(); sr.Close(); } }
3. 删除文件:
//1.1 生成文件名和设置文件物理路径 string phpPath=Server.MapPath(path); if (File.Exists(phpPath)) { File.Delete(phpPath); }
相关文章推荐
- .NET中的文件IO操作实例
- .NET中的文件IO操作实例
- .NET中的文件IO操作实例
- .Net笔记:System.IO之windows文件操作的深入分析
- java io操作文件实例
- .C# 拷贝一个图片到指定文件夹下(IO文件操作实例)
- Java的IO操作(四) - SequenceInputStream类,实例:一个文件分割、合并工具
- BAE之JAVA IO文件操作 BAE之JAVA文件操作实例
- .NET中的IO操作之文件流
- Java的IO操作(四) - SequenceInputStream类,实例:一个文件分割、合并工具
- IO操作练习实例——文件的创建
- Java的IO操作(四) - SequenceInputStream类,实例:一个文件分割、合并工具
- C#文件读写IO操作 实例
- .Net笔记:System.IO之windows文件操作的深入分析
- 使用java.io.*操作文件的拆分与合并,Application一例!
- ajaxwebshop3 .NET表格数据增,删,改操作实例
- GemBox.ExcelLite破解版,可以在没有安装Office的情况,使用.net代码操作Excel文件
- System.IO 路径,文件,目录,I/O常见操作汇总(二)
- System.IO 路径,文件,目录,I/O常见操作汇总(一)
- 实例讲解.NET中资源文件的创建与使用