第一次写随笔,发个文件操作类作为首次吧。FileManager

相关的文件操作都应该可以应用到了。

ContractedBlock.gif ExpandedBlockStart.gif FileManager
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.IO;
  5 using System.Web;
  6 using System.Web.UI.WebControls;
  7 using Microsoft.VisualBasic.FileIO;
  8 
  9 namespace SSS.Components
 10 {
 11     /// <summary>
 12     /// IO操作
 13     /// </summary>
 14     public static class FileManager
 15     {
 16         /// <summary>
 17         /// 上传文件
 18         /// </summary>
 19         /// <param name="targetDir"></param>
 20         /// <param name="fileUpload"></param>
 21         /// <returns></returns>
 22         public static bool UploadFile(string targetDir, FileUpload fileUpload)
 23         {
 24             string fileName = Path.Combine(targetDir, fileUpload.FileName);
 25             if (!File.Exists(fileName))
 26             {
 27                 fileUpload.SaveAs(fileName);
 28                 return true;
 29             }
 30             return false;
 31         }
 32 
 33         /// <summary>
 34         /// 判断指定名称的文件或目录是否存在。
 35         /// </summary>
 36         /// <param name="path"></param>
 37         /// <returns></returns>
 38         public static bool IsEntriesExists(string path)
 39         {
 40             if (Directory.Exists(path)) return true;
 41             if (File.Exists(path)) return true;
 42             return false;
 43         }
 44 
 45         /// <summary>
 46         /// 创建目录
 47         /// </summary>
 48         /// <param name="directoryPath">目录完全路径</param>
 49         public static void CreateDirectory(string directoryPath)
 50         {
 51             if (!Directory.Exists(directoryPath))
 52                 Directory.CreateDirectory(directoryPath);
 53         }
 54 
 55         /// <summary>
 56         ///  重命名文件或文件夹
 57         /// </summary>
 58         /// <param name="path">文件或文件夹的完全路径</param>
 59         /// <param name="newName">新名称,无需路径</param>
 60         /// <returns></returns>
 61         public static void RenameEntries(string path, string newName)
 62         {
 63             if (Directory.Exists(path))
 64             {
 65                 FileSystem.RenameDirectory(path, newName);
 66             }
 67             if (File.Exists(path))
 68             {
 69                 FileSystem.RenameFile(path, newName);
 70             }
 71         }
 72 
 73 
 74 
 75         /// <summary>
 76         /// 删除
 77         /// </summary>
 78         /// <param name="path"></param>
 79         public static void DeleteEntries(string path)
 80         {
 81             if (Directory.Exists(path))
 82             {
 83                 Directory.Delete(path, true);
 84             }
 85             if (File.Exists(path))
 86             {
 87                 File.Delete(path);
 88             }
 89         }
 90 
 91         /// <summary>
 92         /// 返回以KB为单位的文件长度
 93         /// </summary>
 94         /// <param name="fileLength"></param>
 95         /// <returns></returns>
 96         public static string GetKBSize(long fileLength)
 97         {
 98             return (Convert.ToInt32(fileLength / 1024+ 1).ToString() + " KB";
 99         }
100 
101         private static Encoding _globalEncoding = Encoding.GetEncoding("GB2312");
102 
103         public static Encoding GlobalEncoding
104         {
105             get { return _globalEncoding; }
106             set { _globalEncoding = value; }
107         }
108 
109         #region 读写文件
110 
111         public static string ReadFile(string filePath)
112         {
113             return FileManager.ReadFile(filePath, null);
114         }
115 
116         public static string ReadFile(string filePath, Encoding encoding)
117         {
118             if (IsEntriesExists(filePath))
119             {
120                 try
121                 {
122                     string content = encoding != null ? File.ReadAllText(filePath, encoding) : File.ReadAllText(filePath);
123                     return content;
124                 }
125                 catch { }
126             }
127             return string.Empty;
128         }
129 
130         public static bool WriteFile(string filePath, string contents)
131         {
132             return FileManager.WriteFile(filePath, contents, null);
133         }
134 
135         public static bool WriteFile(string filePath, string contents, Encoding encoding)
136         {
137             if (!string.IsNullOrEmpty(filePath))
138             {
139                 try
140                 {
141                     string directoryPath = Path.GetDirectoryName(filePath);
142                     if (!Directory.Exists(directoryPath))
143                         CreateDirectory(directoryPath);
144 
145                     if (encoding != null)
146                         File.WriteAllText(filePath, contents, encoding);
147                     else
148                         File.WriteAllText(filePath, contents);
149                     return true;
150                 }
151                 catch { }
152             }
153             return false;
154         }
155         #endregion
156     }
157 

转载于:https://www.cnblogs.com/eternallove/archive/2009/03/11/1408576.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值