静态生成要在虚拟目录下创建文件夹 来保存生成的页面 那么就要对文件进行操作

一、创建文件夹
using System.IO; string name = "aa";
string path = Server.MapPath("") + "\\" + name;
if (Directory.Exists(path))
{
Response.Write("<script>alert('文件夹已存在了!');history.go(-1);</script>");
}
else
{
DirectoryInfo folder=Directory.CreateDirectory(path);
string time = Convert.ToString(Directory.GetCreationTime(path));
string foldername = name.Substring(name.LastIndexOf("\\") + 1);
Response.Write("添加成功!");
Response.Write("添加时间:"+time);
Response.Write("文件夹名:"+foldername);
} 二、删除文件夹 using System.IO;
string name = "aa";
string path = Server.MapPath("") + "\\" + name;
if (Directory.Exists(path))
{
Directory.Delete(path);
Response.Write("删除成功!");
}
else
{
Response.Write("<script>alert('文件夹不存在!');history.go(-1);</script>");
} 三、文件夹的移动 string name1 = "aa";
string name2 = "bb\\aa";
//移动到的文件夹里,把AA移动到BB里
string path1 = Server.MapPath("") + "\\" + name1;
string path2 = Server.MapPath("") + "\\" + name2;
if (!Directory.Exists(path1))
{
Response.Write("<script>alert('文件夹"+name1+"不存在!');history.go(-1);</script>");
return;
}
if (Directory.Exists(path2))
{
Response.Write("<script>alert('文件夹" + name2 + "已存在!');history.go(-1);</script>");
return;
}
try
{
Directory.Move(path1, path2);
Response.Write("文件夹移动成功!");
}
catch
{
Response.Write("<script>alert('必须在同一目录下操作!');history.go(-1);</script>");
} 四、获取文件夹下的文件列表 前台
<asp:ListBox ID="list" runat="server" Width="200px" Height="300px" Visible="false"></asp:ListBox>
后台
string name = "aa";
string path = Server.MapPath("") + "\\" + name;
if (!Directory.Exists(path))
{
list.Visible = false;
Response.Write("<script>alert('文件夹" + name + "不存在!');history.go(-1);</script>");
return;
}
else
{
DirectoryInfo foldinfo = new DirectoryInfo(path);
FileSystemInfo[] dirs = foldinfo.GetFileSystemInfos();
if (dirs.Length < 1)
{
Response.Write("<script>alert('文件夹中没数据!');history.go(-1);</script>");
return;
}
list.Visible = true;
list.DataSource = dirs;
list.DataBind();
} 五、创建文件
string name = "aa.aspx";
string path = Server.MapPath("") + "\\" + name;
if (File.Exists(path))
{
Response.Write("<script>alert('文件" + name + "已存在!');history.go(-1);</script>");
return;
}
else
{
FileStream fs = File.Create(path); fs.Close();
Response.Write("文件" + name + "添加成功!");
}
六、拷贝文件
string name1 = "aa\\1.html";
string name2 = "bb\\1.html";
string path1 = Server.MapPath("") + "\\" + name1;
string path2 = Server.MapPath("") + "\\" + name2;
if (!File.Exists(path1))
{
Response.Write("<script>alert('文件" + name1 + "不存在!');history.go(-1);</script>");
return;
}
if (File.Exists(path2))
{
Response.Write("<script>alert('文件" + name2 + "已存在!');history.go(-1);</script>");
return;
}
try
{
File.Copy(path1, path2);
Response.Write("拷贝成功!");
}
catch
{
Response.Write("拷贝失败!");
} 七、移动文件
string name1 = "aa\\1.html";
string name2 = "bb\\1.html";
string path1 = Server.MapPath("") + "\\" + name1;
string path2 = Server.MapPath("") + "\\" + name2;
if (!File.Exists(path1))
{
Response.Write("<script>alert('文件" + name1 + "不存在!');history.go(-1);</script>");
return;
}
if (File.Exists(path2))
{
Response.Write("<script>alert('文件" + name2 + "已存在!');history.go(-1);</script>");
return;
}
try
{
File.Move(path1, path2);
Response.Write("移动成功!");
}
catch
{
Response.Write("移动失败!");
} 八、文件删除
string name = "bb\\1.html";
string path = Server.MapPath("") + "\\" + name; if (!File.Exists(path))
{
Response.Write("<script>alert('文件" + name + "不存在!');history.go(-1);</script>");
return;
}
try
{
File.Delete(path);
Response.Write("删除成功!");
}
catch
{
Response.Write("删除失败!");
} 九、获取文件的详细信息
string name = "aa\\11.txt";
string path = Server.MapPath("") + "\\" + name;
if (!File.Exists(path))
{
Response.Write("<script>alert('文件" + name + "不存在!');history.go(-1);</script>");
return;
}
else
{
FileInfo file = new FileInfo(path);
string filexinxi1, filexinxi2, filexinxi3, filexinxi4, filexinxi5;
//文件路径
filexinxi1 = file.FullName;
//文件大小,字节
filexinxi2 = file.Length+"字节";
//文件属性
filexinxi3 = file.Attributes.ToString();
//文件创建时间
filexinxi4 = file.CreationTime.ToShortDateString();
//文件上次访问时间
filexinxi5 = file.LastAccessTime.ToShortDateString();
Response.Write("文件路径:"+filexinxi1+"<br>");
Response.Write("文件大小:" + filexinxi2 + "<br>");
Response.Write("文件属性:" + filexinxi3 + "<br>");
Response.Write("文件创建时间:" + filexinxi4 + "<br>");
Response.Write("文件上次访问时间:" + filexinxi5 + "<br>");
}
十、读取文件内容 string name = "aa\\11.html";
string path = Server.MapPath("") + "\\" + name;
FileInfo fi = new FileInfo(path);
//获取文件名
string filename = fi.Name;
//获取文件扩展名
string extension = fi.Extension;
//判断该文件是否为txt格式
if (extension != ".html")
{
Response.Write("<script>alert('请选择html格式文件!');history.go(-1);</script>");
return;
}
StreamReader sr = new StreamReader(path, System.Text.Encoding.Default);
Response.Write("文件中的内容如下:<br>");
Response.Write(sr.ReadToEnd());
sr.Close();
十一、文件的写入
string name = "aa\\11.html";
string content1="<html><title>大家好</title></html>";
string path = Server.MapPath("") + "\\" + name;
FileInfo fi = new FileInfo(path);
//获取文件名
string filename = fi.Name;
//获取文件扩展名
string extension = fi.Extension;
//判断该文件是否为txt格式
if (extension != ".html")
{
Response.Write("<script>alert('请选择html格式文件!');history.go(-1);</script>");
return;
}
StreamWriter sr = new StreamWriter(path, true, System.Text.Encoding.GetEncoding("gb2312"));
sr.WriteLine(content1);
sr.Close();
Response.Write("文件写入成功!");

asp.net 文件操作小例子(创建文件夹,读,写,删)的更多相关文章

  1. 重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作

    原文:重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作 [源码下载 ...

  2. asp.net在网站根目录下创建文件夹

    假设要在asp.net网站的根目录下建立文件夹hovertree,C#代码如下: string m_keleyiFolderName = Server.MapPath("/hovertree ...

  3. [Xcode 实际操作]七、文件与数据-(2)创建文件夹

    目录:[Swift]Xcode实际操作 本文将演示如何创建文件夹. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class ViewC ...

  4. 强大的pdf文件操作小工具——PDFtk的小白用法 【转载】

    转载出处https://www.cnblogs.com/basterdaidai/p/6204518.html 前言 作为程序员,大家都知道的,总是会被技术小白问各种跟编程没什么关系的硬件.软件问题. ...

  5. python 小兵(4)之文件操作 小问题

    1.光标不对就用seek 2.文件操作方面注意不要变修改变删除,会爆出文件正在运行不能操作 3.w模式下只有开始打开的时候会清空 4.文件操作的时候用as 后面的参数进行操作,不能用文件名进行操作 5 ...

  6. 【温故而知新:文件操作】C#的文件读写相关

    StreamReader类以及其方法ReadLine,Read,ReadToEnd的分析 首先StreamReader类的构造参数非常丰富在这里,我觉得最常用的就是StreamReader(Strea ...

  7. 《Java核心技术卷二》笔记(二)文件操作和内存映射文件

    文件操作 上一篇已经总结了流操作,其中也包括文件的读写.文件系统除了读写以为还有很多其他的操作,如复制.移动.删除.目录浏览.属性读写等.在Java7之前,一直使用File类用于文件的操作.Java7 ...

  8. 第九天- 文件操作 r w a 文件复制/修改

    文件操作简介:使用python来读写文件是非常简单的操作.我们使用 open() 函数来打开一个文件,获取到文件句柄.然后通过文件句柄就可以进行各种各样的操作了.根据打开⽅方式的不同能够执行的操作也会 ...

  9. python对文件操作 r w a 文件复制/修改

    文件操作简介: 使用python来读写文件是非常简单的操作.我们使用 open() 函数来打开一个文件,获取到文件句柄.然后 通过文件句柄就可以进行各种各样的操作了.根据打开⽅方式的不同能够执行的操作 ...

随机推荐

  1. ADO.NET 快速入门(十四):使用 SQL Server 检索数据

    SqlDataReader 类提供了一种从数据源读取数据记录只进流的方法.如果想使用 OLE DB 接口的数据库或者 SQL Server7.0 之前的版本,请参考文章:使用 OLE DB 检索数据. ...

  2. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) A. Bear and Poker 分解

    A. Bear and Poker Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/573/pro ...

  3. 进程和cpu的相关知识和简单调优方案

    进程就是一段执行的程序,每当一个程序执行时.对于操作系统本身来说,就创建了一个进程,而且分配了相应的资源.进程能够分为3个类别:     1.交互式进程(I/O)     2.批处理进程 (CPU) ...

  4. SkinSharp用法

    SkinSharp又称Skin#,是很好用的一款轻量化的VC程序美化工具 官网地址是http://www.skinsharp.com/ 尽管SkinSharp是收费软件,但提供试用版,并且比較厚道,试 ...

  5. asp.net mvc控制器动作体返回ImageResult,可作验证码

    public ActionResult Img() { // 获取博客园空间顶部的banner图片 WebRequest req = WebRequest.Create("http://sp ...

  6. hibernate 实体关系映射笔记

    @经常使用属性说明:     @Entity:实体类     @Table:指定相应数据表     @Id:主键,使用能够为null值的类型,假设实体类没有保存到数据库是一个暂时状态     @Col ...

  7. JAVA static 作用

    static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...

  8. Java内部类的自我理解

    本文借鉴网络上多位大牛的博客和文章.感谢各位不知名人士的分享. 一.什么事内部类? 内部类是指在一个外部类的内部再定义一个类.内部类作为外部类的成员,而且依附于外部类而存在的.内部类能够为静态,可用p ...

  9. android151 笔记 3

    34. 对android虚拟机的理解,包括内存管理机制垃圾回收机制. 虚拟机很小,空间很小,谈谈移动设备的虚拟机的大小限制 16M , 谈谈加载图片的时候怎么处理大图片的,压缩. 垃圾回收,没有引用的 ...

  10. MySQL锁系列3 MDL锁

    http://www.cnblogs.com/xpchild/p/3790139.html   MySQL为了保护数据字典元数据,使用了metadata lock,即MDL锁,保证在并发的情况下,结构 ...