using System;
using System.Web;
using System.Configuration;
namespace Baolee.GeneralMethod
{
/// <summary>
/// PublicConst 的摘要说明。
/// </summary>
public class PublicConst
{
#region // Methods
/// <summary>
/// 构造函数
/// </summary>
public PublicConst()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 时间字串, 同 javascript 中的 d.valueOf() 长整型字串 自1970-1-1 00:00:00开始的毫秒数
/// </summary>
/// <returns></returns>
public static string DataValueString()
{
long num = DateTime.Now.Subtract(new DateTime(0x7b2, 1, 1, 0, 0, 0, 0)).Ticks / ((long) 0x2710);
return num.ToString();
}
/// <summary>
/// 计算几天 前/后 的日期
/// </summary>
/// <param name="num">天数 +/-</param>
/// <returns>eg: 20030911</returns>
public static string DateCalForDay(int num)
{
DateTime now = DateTime.Now;
int num2 = num;
return now.AddDays((double) num2).ToString("yyyyMMdd");
}
/// <summary>
/// 计算几天 前/后 的日期 DateCalForDay(3); 20060608 DateCalForDay(3,@"//"); 2006/06/08 DateCalForDay(3,@"//"); 2006/06/08 DateCalForDay(3,"::"); 2006::06::08 DateCalForDay(3,"|"); 2006|06|08 DateCalForDay("20060605",3); 20060608
/// </summary>
/// <param name="num">天数 +/-</param>
/// <param name="compChar">分隔符</param>
/// <returns>eg: 2003/09/11</returns>
public static string DateCalForDay(int num, string compChar)
{
DateTime now = DateTime.Now;
string format = "yyyy" + compChar + "MM" + compChar + "dd";
int num2 = num;
return now.AddDays((double) num2).ToString(format);
}
/// <summary>
/// 计算日期,暂时为 yyyyMMdd格式提供服务,
/// </summary>
/// <param name="sourYmd"></param>
/// <param name="n"></param>
/// <returns>yyyyMMdd</returns>
public static string DateCalForDay(string sourYmd, int n)
{
return Convert.ToDateTime(sourYmd.Substring(0, 4) + "-" + sourYmd.Substring(4, 2) + "-" + sourYmd.Substring(6, 2)).AddDays((double) n).ToString("yyyyMMdd");
}
/// <summary>
/// 计算月,以now为基准
/// </summary>
/// <param name="num"></param>
/// <returns>yyyy/MM</returns>
public static string DateCalForMonth(int num)
{
return DateTime.Now.AddMonths(num).ToString(@"yyyy//MM");
}
/// <summary>
/// 计算月,暂时为 yyyyMM格式提供服务,
/// </summary>
/// <param name="sourYm"></param>
/// <param name="n"></param>
/// <returns>yyyyMM</returns>
public static string DateCalForMonth(string sourYm, int n)
{
return Convert.ToDateTime(sourYm.Substring(0, 4) + "-" + sourYm.Substring(4, 2) + "-01").AddMonths(n).ToString("yyyyMM");
}
/// <summary>
/// 计算月,以now为基准
/// </summary>
/// <param name="num"></param>
/// <returns>yyyyMMdd</returns>
public static string DateCalForMonth8(int num)
{
return DateCalForMonth(num).Replace("/", "");
}
/// <summary>
/// 返回上一个月,YYYYMM
/// </summary>
/// <returns></returns>
public static string DatePrevMonthStr()
{
return DatePrevMonthStr("");
}
/// <summary>
/// 返回上一个月 ,格式根据 disPartChar而定
/// </summary>
/// <param name="disPartChar">if "" then yyyyMM else yyyy"char"MM </param>
/// <returns></returns>
public static string DatePrevMonthStr(string disPartChar)
{
DateTime now = DateTime.Now;
if (disPartChar == "")
{
return now.AddMonths(-1).ToString("yyyyMM");
}
string format = @"yyyy/" + disPartChar + "MM";
return now.AddMonths(-1).ToString(format);
}
/// <summary>
/// 是否法定的文件类型
/// </summary>
/// <param name="filetype">包括".gif",".jpg",".jpeg",".max",".tiff",".zip",".rar",".bmp"</param>
/// <returns></returns>
public static bool FilterFileType(string filetype)
{
string[] textArray = new string[] { ".gif", ".jpg", ".jpeg", ".max", ".tiff", ".zip", ".rar", ".bmp" };
foreach (string text in textArray)
{
if (filetype.ToLower() == text)
{
return true;
}
}
return false;
}
/// <summary>
/// Server.MapPath;
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string MapPath(string fileName)
{
return HttpContext.Current.Server.MapPath(fileName);
}
/// <summary>
/// 取得一串由数字组成的无序字串
/// </summary>
/// <param name="length">所需字串的长度</param>
/// <returns></returns>
public static string RandomByNumber(int length)
{
return RandomByNumber(length, 0);
}
/// <summary>
///
/// </summary>
/// <param name="length"></param>
/// <param name="seed"></param>
/// <returns></returns>
public static string RandomByNumber(int length, int seed)
{
Random random;
string[] textArray = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
string text = "";
if (seed == 0)
{
random = new Random();
}
else
{
random = new Random(seed);
}
for (int i = 0; i < length; i++)
{
text = text + textArray[random.Next(textArray.Length - 1)];
}
return text;
}
/// <summary>
/// 取得一串由数字和字母组成的无序字串
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string RandomByStr(int length)
{
return RandomByStr(length, 0);
}
/// <summary>
/// 取得一串由数字和字母组成的无序字串
/// </summary>
/// <param name="length">所需字串的长度</param>
/// <param name="seed">种子</param>
/// <returns></returns>
public static string RandomByStr(int length, int seed)
{
string[] textArray = new string[] {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
};
string text = "";
Random random = new Random();
if (seed == 0)
{
random = new Random();
}
else
{
random = new Random(seed);
}
for (int i = 0; i < length; i++)
{
text = text + textArray[random.Next(textArray.Length - 1)];
}
return text;
}
/// <summary>
/// UBB可以识别的文件类型 case ".gif" : case ".jpg" : case ".jpeg" : case ".bmp" :UploadFileType="IMG"; break;case ".zip" :UploadFileType="ZIP"; break; case ".rar" : UploadFileType="RAR"; break; default : UploadFileType="UPLOAD"; break;
/// </summary>
/// <param name="filetype"></param>
/// <returns></returns>
public static string UBBFileType(string filetype)
{
switch (filetype.ToLower())
{
case ".gif":
case ".jpg":
case ".jpeg":
case ".bmp":
return "IMG";
case ".zip":
return "ZIP";
case ".rar":
return "RAR";
}
return "UPLOAD";
}
#endregion
#region // Properties
/// <summary>
/// 数据库连接字串[在Web.Config中设定] AppSettings["OracleConnStr"]
/// </summary>
public static string ConnectionString
{
get
{
return ConfigurationSettings.AppSettings["OracleConnStr"].ToString();
}
}
/// <summary>
/// 当前月份 eg:200404
/// </summary>
public static string DateNowMonth
{
get
{
return DateTime.Now.ToString("yyyyMM");
}
}
/// <summary>
/// Today 2003/09/11
/// </summary>
public static string DateNowToday
{
get
{
return DateTime.Now.ToString(@"yyyy//MM//dd");
}
}
/// <summary>
/// Today 20030911
/// </summary>
public static string DateNowToday8
{
get
{
return DateTime.Now.ToString("yyyyMMdd");
}
}
/// <summary>
/// 上一个月的年月 eg:2003/09
/// </summary>
public static string DatePrevMonth
{
get
{
return DateTime.Now.AddMonths(-1).ToString(@"yyyy//MM");
}
}
/// <summary>
/// 记录错误信息的物理路径[在Web.Config中设定] AppSettings["ErrLogPath"]
/// </summary>
public static string ErrLogPath
{
get
{
return ConfigurationSettings.AppSettings["ErrLogPath"].ToString();
}
}
/// <summary>
/// 当前页面的URL
/// </summary>
public static string HistoryUrl
{
get
{
return HttpContext.Current.Request.Url.AbsoluteUri;
}
}
/// <summary>
/// 设定每月21号以后为下个月,用于计算应收时的费用月份
/// </summary>
public static string LogicPrevYm
{
get
{
if (DateTime.Now.Day > 0x15)
{
return DateNowMonth;
}
return DatePrevMonthStr("");
}
}
/// <summary>
/// Mail服务器地址[在Web.Config中设定] AppSettings["MailServer"]
/// </summary>
public static string MailServer
{
get
{
return ConfigurationSettings.AppSettings["MailServer"].ToString();
}
}
/// <summary>
/// 管理员EMail[在Web.Config中设定] AppSettings["ManagerEmail"]
/// </summary>
public static string ManagerEmail
{
get
{
return ConfigurationSettings.AppSettings["ManagerEmail"].ToString();
}
}
/// <summary>
/// 当前页面完整URL
/// </summary>
public static string PageUrl
{
get
{
return HttpContext.Current.Request.Url.ToString();
}
}
/// <summary>
/// return yMMddhhmmssf
/// </summary>
public static string RandomByTime12
{
get
{
return DateTime.Now.ToString("yyyyMMddHHmmssf").Substring(3, 12);
}
}
/// <summary>
/// return yyyyMMddhhmmss
/// </summary>
public static string RandomByTime14
{
get
{
return DateTime.Now.ToString("yyyyMMddHHmmss");
}
}
/// <summary>
/// 取得一个由时间序组成的6位有序字串日时分秒毫2
/// </summary>
public static string RandomByTime6
{
get
{
string[] textArray = new string[] {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
};
DateTime now = DateTime.Now;
return (textArray[now.Day] + textArray[now.Hour] + textArray[now.Minute] + textArray[now.Second] + textArray[now.Millisecond / 100] + textArray[now.Millisecond % 60]);
}
}
/// <summary>
/// return hhmmssff
/// </summary>
public static string RandomByTime8
{
get
{
return DateTime.Now.ToString("HHmmssff");
}
}
/// <summary>
/// 用户IP
/// </summary>
public static string RemoteUserIp
{
get
{
return HttpContext.Current.Request.UserHostAddress.ToString();
}
}
/// <summary>
/// 程式根目录物理路径
/// </summary>
public static string RootPath
{
get
{
return HttpContext.Current.Request.PhysicalApplicationPath;
}
}
/// <summary>
/// return YMMddHmmssf
/// </summary>
public static string SaveAsFileName
{
get
{
DateTime now = DateTime.Now;
return (now.ToString("yMMdd") + now.ToString("Hmmssf"));
}
}
/// <summary>
/// File Path&FileName No extend name
/// </summary>
public static string SaveAsFilePathAndFileName
{
get
{
return (UpLoadPath + SaveAsFileName).ToLower();
}
}
/// <summary>
/// 上传档案至服务器的物理路径[在Web.Config中设定] AppSettings["UploadPath"]
/// </summary>
public static string UpLoadPath
{
get
{
return ConfigurationSettings.AppSettings["UploadPath"].ToString();
}
}
/// <summary>
/// Web服务器地址[在Web.Config中设定] AppSettings["WebServer"]
/// </summary>
public static string WebServer
{
get
{
return ConfigurationSettings.AppSettings["WebServer"].ToString();
}
}
/// <summary>
/// 星期日 至 星期六
/// </summary>
public static string[] WeekName
{
get
{
return new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
}
}
/// <summary>
/// 日 至 六
/// </summary>
public static string[] WeekName2
{
get
{
return new string[] { "日", "一", "二", "三", "四", "五", "六" };
}
}
#endregion
}
}