相关的文件操作都应该可以应用到了。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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