温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:NETCMSv1.5(Build0509)完整源码版
当前文件路径:NetCMSv15/NetCMS.Content/Templet/Templet.cs

1using System; 2
using System.Collections.Generic; 3
using System.Text; 4
using System.IO; 5
6
namespace NetCMS.Content.Templet 7
{ 8
public class Templet 9
{ 10
/// <summary> 11
/// 修改文件名(文件夹)名称 12
/// </summary> 13
/// <param name="path">路径</param> 14
/// <param name="oldname">原始名称</param> 15
/// <param name="newname">新名称</param> 16
/// <param name="type">0为文件夹,1为文件</param> 17
/// <returns>成功返回1</returns> 18
19
public int EidtName(string path, string oldname, string newname, int type) 20
{ 21
int result = 0; 22
if (type == 0) 23
{ 24
if (Directory.Exists(path + "\\" + oldname)) 25
{ 26
try 27
{ 28
Directory.Move(path + "\\" + oldname, path + "\\" + newname.Replace(".", "")); 29
} 30
catch(IOException e) 31
{ 32
throw new Exception(e.ToString()); 33
} 34
result = 1; 35
} 36
else 37
{ 38
throw new Exception("参数传递错误!"); 39
} 40
} 41
else 42
{ 43
if (File.Exists(path + "\\" + oldname)) 44
{ 45
try 46
{ 47
File.Move(path + "\\" + oldname, path + "\\" + newname); 48
} 49
catch(Exception e) 50
{ 51
throw new Exception(e.ToString()); 52
} 53
result = 1; 54
} 55
else 56
{ 57
throw new Exception("参数传递错误!"); 58
} 59
} 60
return result; 61
} 62
63
64
65
/// <summary> 66
/// 删除文件或文件夹 67
/// </summary> 68
/// <param name="path">路径</param> 69
/// <param name="filename">名称</param> 70
/// <param name="type">0代表文件夹,1代表文件</param> 71
/// <returns>返回值</returns> 72
73
74
public int Del(string path,string filename, int type) 75
{ 76
int result = 0; 77
if (type == 0) 78
{ 79
if (Directory.Exists(path + "\\" + filename)) 80
{ 81
try 82
{ 83
Directory.Delete(path, true); 84
} 85
catch (Exception e) 86
{ 87
throw new IOException(e.ToString()); 88
} 89
result = 1; 90
} 91
else 92
{ 93
throw new IOException("参数错误!"); 94
} 95
} 96
else 97
{ 98
if (File.Exists(path + "\\" + filename)) 99
{ 100
try 101
{ 102
File.Delete(path + "\\" + filename); 103
} 104
catch (Exception e) 105
{ 106
throw new IOException(e.ToString()); 107
} 108
result = 1; 109
} 110
else 111
{ 112
throw new IOException("参数错误!"); 113
} 114
} 115
return result; 116
} 117
118
/// <summary> 119
/// 添加文件夹 120
/// </summary> 121
/// <param name="path">当前路径</param> 122
/// <param name="filename">文件夹名称</param> 123
/// <returns></returns> 124
125
public int AddDir(string path, string filename) 126
{ 127
int result = 0; 128
if (Directory.Exists(path + "\\" + filename)) 129
{ 130
throw new IOException("此文件夹已存在!"); 131
} 132
else 133
{ 134
try 135
{ 136
Directory.CreateDirectory(path + "\\" + filename.Replace(".", "")); 137
} 138
catch (IOException e) 139
{ 140
throw new IOException(e.ToString()); 141
} 142
result = 1; 143
} 144
return result; 145
} 146
/// <summary> 147
/// 获取当前目录的父目录 148
/// </summary> 149
/// <param name="path">当前目录</param> 150
/// <param name="temppath">当前的模板目录</param> 151
/// <returns></returns> 152
153
public string PathPre(string path,string temppath) 154
{ 155
if (path != null) 156
{ 157
int i, j; 158
i = path.LastIndexOf(temppath); 159
j = path.Length - i; 160
path = path.Substring(i, j); 161
} 162
else 163
{ 164
path = temppath; 165
} 166
return path; 167
} 168
169
/// <summary> 170
/// 保存文件 171
/// </summary> 172
/// <param name="path">路径</param> 173
/// <param name="fileContent">文件内容</param> 174
/// <returns></returns> 175
176
public int saveFile(string path, string fileContent) 177
{ 178
int result = 0; 179
if (File.Exists(path)) 180
{ 181
try 182
{ 183
StreamWriter Fso = new StreamWriter(path); 184
Fso.WriteLine(fileContent); 185
Fso.Close(); 186
Fso.Dispose(); 187
} 188
catch (IOException e) 189
{ 190
throw new IOException(e.ToString()); 191
} 192
result = 1; 193
} 194
else 195
{ 196
throw new Exception("文件已经被删除!"); 197
} 198
return result; 199
} 200
201
/// <summary> 202
/// 显示文件内容 203
/// </summary> 204
/// <param name="path">文件路径</param> 205
/// <returns></returns> 206
207
public string showFileContet(string path) 208
{ 209
string str_content = ""; 210
if (File.Exists(path)) 211
{ 212
try 213
{ 214
StreamReader Fso = new StreamReader(path); 215
str_content = Fso.ReadToEnd(); 216
Fso.Close(); 217
Fso.Dispose(); 218
} 219
catch (IOException e) 220
{ 221
throw new IOException(e.ToString()); 222
} 223
} 224
else 225
{ 226
throw new Exception("找不到相应的文件!"); 227
} 228
return str_content; 229
} 230
} 231
} 232





}