1.生成Html文件,文件名随机生成
string fileName = System.Guid.NewGuid().ToString() + ".html";
string FilePath = "e:/User/" + fileName ;//HTML文本路径
FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate);//创建一个HTML文本
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("gb2312"));
sw.WriteLine("输入Html文本中的内容");
sw.Close();
如果文件路径是动态生成的,一定要先将目录生成好,否则会报错:
//生成相关文件夹
string path = "Html/" + UserID.ToString() + "/";
string FilePath = "e:/User/" + path;//文件保存路径
System.IO.Directory.CreateDirectory(FilePath);
string FolderPath = FilePath + "/images";//图片保存路径
System.IO.Directory.CreateDirectory(FolderPath);
2.删除文件夹
http://developer.51cto.com/art/200908/144236.htm
3.删除文件
http://zhidao.baidu.com/link?url=4pasa_TB40dasHEWyGWJ_q1VeoMhHtbhDNZLxfuHZWWwaWT7yiOaMkBtll9SV0wco8PJ2xvmLvISqcN2P2aNLK
string fileToDelete = "C:\test.txt";
try
{
If (System.IO.File.Exist(fileToDelete) == true)
{
System.IO.File.Delete(fileToDelete);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
4.file和directory区别
http://www.cnblogs.com/yank/archive/2007/11/16/961747.html
File —— 静态实用类,提供许多静态方法,用于移动、复制和删除文件。
Directory —— 静态实用类,提供许多静态方法,用于移动、复制和删除目录。