asp.net 文件的操作

最近稍微对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();
       }

   }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值