最近稍微对Asp.net文件操作的做了以下总结,下面贴出让大家共享
using
System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
namespace Tjjj
... {
/**//// <summary>
/// 文件操作类
/// </summary>
public class FileFuncation
...{
/**//// <summary>
/// 删除文件
/// </summary>
/// <param name="name">文件加名称</param>
public static void DelFile(string name)
...{
File.Delete(HttpContext.Current.Server.MapPath(name));
}
/**//// <summary>
/// 删除文件夹
/// </summary>
/// <param name="name">文件夹名称</param>
public static void DelFolder(string name)
...{
Directory.Delete(HttpContext.Current.Server.MapPath(name), true);
}
/**//// <summary>
/// 新建文件夹
/// </summary>
/// <param name="str">文件夹路径及保存名称</param>
public static void Newfolder(string str)
...{
string text1 = "";
string[] textArray1 = str.Split(new char[] ...{ '/' });
for (int num1 = 0; num1 < textArray1.Length; num1++)
...{
text1 = text1 + textArray1[num1] + "/";
if (!Directory.Exists(HttpContext.Current.Server.MapPath(text1)))
...{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(text1));
}
}
}
/**//// <summary>
/// 读取文件
/// </summary>
/// <param name="fileName">文件名称</param>
/// <returns></returns>
public static string ReadFile(string fileName)
...{
string strContent=string.Empty;
if (File.Exists(HttpContext.Current.Server.MapPath(fileName)))
...{
Encoding fileEncoding = TxtFileEncoding.GetEncoding(HttpContext.Current.Server.MapPath(fileName), Encoding.GetEncoding("GB2312"));//取得这txt文件的编码
StreamReader reader1 = new StreamReader(HttpContext.Current.Server.MapPath(fileName),fileEncoding);
strContent = reader1.ReadToEnd();
reader1.Close();
}
return strContent;
}
/**//// <summary>
/// 保存文件
/// </summary>
/// <param name="filename">文件名称</param>
/// <param name="content">保存的数据</param>
public static void SaveFile(string filename,string content)
...{
StreamWriter sr = new StreamWriter(HttpContext.Current.Server.MapPath(filename),false,Encoding.GetEncoding("GB2312"));
sr.WriteLine(content);
sr.Close();
}
/**//// <summary>
/// 创建文件
/// </summary>
/// <param name="filePath">文件路径/文件名</param>
/// <param name="strContent"></param>
public static void CreatFile(string filePath,string strContent)
...{
StreamWriter sr = new StreamWriter(HttpContext.Current.Server.MapPath(filePath));
sr.Write(strContent);
sr.Close();
}
/**//// <summary>
/// 移动文件
/// </summary>
/// <param name="Name">文件名</param>
/// <param name="DestName">移动后的文件名</param>
public static void ReName(string Name, string DestName)
...{
File.Move(HttpContext.Current.Server.MapPath(Name), HttpContext.Current.Server.MapPath(DestName));
}
/**//// <summary>
/// 文件下载
/// </summary>
/// <param name="path">文件保存路径</param>
public static void DownLoad(string path)
...{
System.IO.FileInfo fi = new System.IO.FileInfo(path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(path)));
HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.WriteFile(path);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
namespace Tjjj
... {
/**//// <summary>
/// 文件操作类
/// </summary>
public class FileFuncation
...{
/**//// <summary>
/// 删除文件
/// </summary>
/// <param name="name">文件加名称</param>
public static void DelFile(string name)
...{
File.Delete(HttpContext.Current.Server.MapPath(name));
}
/**//// <summary>
/// 删除文件夹
/// </summary>
/// <param name="name">文件夹名称</param>
public static void DelFolder(string name)
...{
Directory.Delete(HttpContext.Current.Server.MapPath(name), true);
}
/**//// <summary>
/// 新建文件夹
/// </summary>
/// <param name="str">文件夹路径及保存名称</param>
public static void Newfolder(string str)
...{
string text1 = "";
string[] textArray1 = str.Split(new char[] ...{ '/' });
for (int num1 = 0; num1 < textArray1.Length; num1++)
...{
text1 = text1 + textArray1[num1] + "/";
if (!Directory.Exists(HttpContext.Current.Server.MapPath(text1)))
...{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(text1));
}
}
}
/**//// <summary>
/// 读取文件
/// </summary>
/// <param name="fileName">文件名称</param>
/// <returns></returns>
public static string ReadFile(string fileName)
...{
string strContent=string.Empty;
if (File.Exists(HttpContext.Current.Server.MapPath(fileName)))
...{
Encoding fileEncoding = TxtFileEncoding.GetEncoding(HttpContext.Current.Server.MapPath(fileName), Encoding.GetEncoding("GB2312"));//取得这txt文件的编码
StreamReader reader1 = new StreamReader(HttpContext.Current.Server.MapPath(fileName),fileEncoding);
strContent = reader1.ReadToEnd();
reader1.Close();
}
return strContent;
}
/**//// <summary>
/// 保存文件
/// </summary>
/// <param name="filename">文件名称</param>
/// <param name="content">保存的数据</param>
public static void SaveFile(string filename,string content)
...{
StreamWriter sr = new StreamWriter(HttpContext.Current.Server.MapPath(filename),false,Encoding.GetEncoding("GB2312"));
sr.WriteLine(content);
sr.Close();
}
/**//// <summary>
/// 创建文件
/// </summary>
/// <param name="filePath">文件路径/文件名</param>
/// <param name="strContent"></param>
public static void CreatFile(string filePath,string strContent)
...{
StreamWriter sr = new StreamWriter(HttpContext.Current.Server.MapPath(filePath));
sr.Write(strContent);
sr.Close();
}
/**//// <summary>
/// 移动文件
/// </summary>
/// <param name="Name">文件名</param>
/// <param name="DestName">移动后的文件名</param>
public static void ReName(string Name, string DestName)
...{
File.Move(HttpContext.Current.Server.MapPath(Name), HttpContext.Current.Server.MapPath(DestName));
}
/**//// <summary>
/// 文件下载
/// </summary>
/// <param name="path">文件保存路径</param>
public static void DownLoad(string path)
...{
System.IO.FileInfo fi = new System.IO.FileInfo(path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(path)));
HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.WriteFile(path);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
}