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

1//====================================================== 2
//== (c)2008 aspxcms inc by NeTCMS v1.0 == 3
//== Forum:bbs.aspxcms.com == 4
//== Website:www.aspxcms.com == 5
//====================================================== 6
using System; 7
using System.Data; 8
using System.Configuration; 9
using System.IO; 10
using System.Web; 11
using System.Web.Security; 12
using System.Web.UI; 13
using System.Web.UI.WebControls; 14
using System.Web.UI.WebControls.WebParts; 15
using System.Web.UI.HtmlControls; 16
using System.Xml; 17
using System.Text.RegularExpressions; 18
using NetCMS.Global; 19
using System.Net.Mail; 20
21
namespace NetCMS.Common 22
{ 23
/// <summary> 24
/// Class1 的摘要说明 25
/// </summary> 26
public class Public 27
{ 28
29
/// <summary> 30
/// 得到站点用户IP, IpSTR = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString() 31
/// </summary> 32
/// <returns></returns> 33
public static string getUserIP() 34
{ 35
return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); 36
} 37
38
/// <summary> 39
/// 去除字符串最后一个','号 40
/// </summary> 41
/// <param name="chr">:要做处理的字符串</param> 42
/// <returns>返回已处理的字符串</returns> 43
/// /// CreateTime:2007-03-26 Code By DengXi 44
public static string Lost(string chr) 45
{ 46
if (chr == null || chr == string.Empty) 47
{ 48
return ""; 49
} 50
else 51
{ 52
chr = chr.Remove(chr.LastIndexOf(",")); 53
return chr; 54
} 55
} 56
57
// 58
public static string lostfirst(string chr) 59
{ 60
string flg = ""; 61
if (chr != string.Empty || chr != null) 62
{ 63
if (chr.Substring(0, 1) == "/") 64
flg = chr.Replace(chr.Substring(0, 1), ""); 65
else 66
flg = chr; 67
} 68
return flg; 69
} 70
71
//public static void sendMail(string smtpserver,string userName,string pwd, string strfrom, string strto, string subj, string bodys) 72
//{ 73
// SmtpClient mail = new System.Net.Mail.SmtpClient(); 74
// mail.Host = smtpserver;//smtp 75
// mail.Credentials = new System.Net.NetworkCredential(userName, pwd); 76
// //mail.Credentials.GetCredential = new System.Net.NetworkCredential(userName, pwd); ; 77
// //mail.EnableSsl = ssl;//发送连接套接层是否加密 例如用gmail发是加密的 78
// System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strfrom, strto); 79
// message.Body = bodys; 80
// message.Subject = subj; 81
// message.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312"); 82
// message.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312"); 83
// message.IsBodyHtml = true; 84
// //message. 85
// mail.Send(message); 86
//} 87
88
/// <summary> 89
/// 发送电子邮件 90
/// </summary> 91
/// <param name="from"></param> 92
/// <param name="to"></param> 93
/// <param name="subj"></param> 94
/// <param name="bodys"></param> 95
96
public static void sendMail(string smtpserver, string userName, string pwd, string strfrom, string strto, string subj, string bodys) 97
{ 98
SmtpClient _smtpClient = new SmtpClient(); 99
_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 100
_smtpClient.Host = smtpserver;//指定SMTP服务器 101
_smtpClient.Credentials = new System.Net.NetworkCredential(userName, pwd);//用户名和密码 102
103
MailMessage _mailMessage = new MailMessage(strfrom, strto); 104
_mailMessage.Subject = subj;//主题 105
_mailMessage.Body = bodys;//内容 106
_mailMessage.BodyEncoding = System.Text.Encoding.Default;//正文编码 107
_mailMessage.IsBodyHtml = true;//设置为HTML格式 108
_mailMessage.Priority = MailPriority.High;//优先级 109
_smtpClient.Send(_mailMessage); 110
} 111
112
113
/// <summary> 114
/// 读取web.config相关数据信息 115
/// </summary> 116
/// <param name="xmlTargetElement">相关字节</param> 117
/// <returns></returns> 118
/// 编写时间2007-03-08 y.xiaobin(著) 119
public static string getXmlElementValue(string xmlTargetElement) 120
{ 121
return System.Configuration.ConfigurationManager.AppSettings[xmlTargetElement]; 122
} 123
124
/// <summary> 125
/// web.config相关文件操作 126
/// 0检测是web.config是否为只读或可写;返回值为:true或false,1把web.config改写为只读;2把web.config改写为可写 127
/// 在此函数中自动去根目下寻找web.config 128
/// </summary> 129
/// <param name="flg">0检测是web.config是否为只读或可写;返回值为:true或false,1把web.config改写为只读;2把web.config改写为可写</param> 130
/// 2007-5-9 y.xiaobin 131
/// <returns></returns> 132
public static bool constReadOnly(int num) 133
{ 134
bool _readonly = false; 135
string _config = HttpContext.Current.Server.MapPath(@"~/Web.config"); 136
FileInfo fi = new FileInfo(_config); 137
switch (num) 138
{ 139
case 0: _readonly = fi.IsReadOnly; break; 140
case 1: 141
fi.IsReadOnly = true; 142
_readonly = true; 143
break; 144
case 2: 145
{ 146
fi.IsReadOnly = false; 147
_readonly = false; 148
} 149
break; 150
default: throw new Exception("错误参数!"); 151
} 152
153
return _readonly; 154
155
} 156
157
/// <summary> 158
/// web.config相关文件操作 159
/// 0检测是web.config是否为只读或可写;返回值为:true或false,1把config改写为只读;2把web.config改写为可写 160
/// 在此函数中自动去根目下寻找web.config 161
/// </summary> 162
/// <param name="flg">0检测是web.config是否为只读或可写;返回值为:true或false,1把web.config改写为只读;2把web.config改写为可写</param> 163
/// 2007-5-9 y.xiaobin 164
/// <returns></returns> 165
public static bool constReadOnly(int num,string strSource) 166
{ 167
bool _readonly = false; 168
string _config = HttpContext.Current.Server.MapPath(@"~/" + strSource); 169
FileInfo fi = new FileInfo(_config); 170
switch (num) 171
{ 172
case 0: _readonly = fi.IsReadOnly; break; 173
case 1: 174
fi.IsReadOnly = true; 175
_readonly = true; 176
break; 177
case 2: 178
{ 179
fi.IsReadOnly = false; 180
_readonly = false; 181
} 182
break; 183
default: throw new Exception("错误参数!"); 184
} 185
186
return _readonly; 187
188
} 189
/// <summary> 190
/// 保存web.config设置 191
/// </summary> 192
/// <param name="xmlTargetElement">关键字</param> 193
/// <param name="xmlText">value</param> 194
/// 2007.05.09 修改 y.xiaobin 195
public static void SaveXmlElementValue(string xmlTargetElement, string xmlText) 196
{ 197
string returnInt = null; 198
string filename = HttpContext.Current.Server.MapPath("~") + @"/Web.config"; 199
XmlDocument xmldoc = new XmlDocument(); 200
xmldoc.Load(filename); 201
XmlNodeList topM = xmldoc.DocumentElement.ChildNodes; 202
foreach (XmlNode element in topM) 203
{ 204
if (element.Name == "appSettings") 205
{ 206
XmlNodeList node = element.ChildNodes; 207
if (node.Count > 0) 208
{ 209
foreach (XmlNode el in node) 210
{ 211
if (el.Name == "add") 212
{ 213
if (el.Attributes["key"].InnerXml == xmlTargetElement) 214
{ 215
//保存web.config数据 216
el.Attributes["value"].Value = xmlText; 217
xmldoc.Save(HttpContext.Current.Server.MapPath(@"~/Web.config")); 218
return; 219
} 220
} 221
} 222
} 223
else 224
{ 225
returnInt = "Web.Config配置文件未配置"; 226
} 227
break; 228
} 229
else 230
{ 231
returnInt = "Web.Config配置文件未配置"; 232
} 233
} 234
235
if (returnInt != null) 236
throw new Exception(returnInt); 237
} 238
239
240
241
/// <summary> 242
/// 删除文件夹,文件 243
/// </summary> 244
/// <param name="DirPath">文件夹路径</param> 245
/// <param name="FilePath">文件路径</param> 246
/// <returns>删除</returns> 247
/// /// CreateTime:2007-03-28 Code By DengXi 248
public static void DelFile(string DirPath, string FilePath) 249
{ 250
try 251
{ 252
if (System.IO.File.Exists(FilePath)) 253
{ 254
System.IO.File.Delete(FilePath); 255
} 256
if (System.IO.Directory.Exists(DirPath)) 257
{ 258
System.IO.Directory.Delete(DirPath); 259
} 260
} 261
catch { } 262
} 263
264
/// <summary> 265
/// 得到SQL语句的SiID;getstr = " and SiteID='" + NetCMS.Global.Current.SiteID + "'" 266
/// </summary> 267
/// <returns></returns> 268
public static string getSessionStr() 269
{ 270
string getstr = ""; 271
if (NetCMS.Global.Current.SiteID != "0"){getstr = " and SiteID='" + NetCMS.Global.Current.SiteID + "'";} 272
return getstr; 273
} 274
275
/// <summary> 276
/// 得到频道ID; and ChannelID='" + NetCMS.Global.Current.SiteID + "' 277
/// </summary> 278
/// <returns></returns> 279
public static string getCHStr() 280
{ 281
string getstr = ""; 282




