/// <summary> /// 随机获取IPv4地址 /// </summary> public static string IPv4() { List <int> list = new List <int>(); for (int i = 0; i < 4; i++) { list.Add(GetInt(0, 255 + 1)); } return(ConvertTool.ToString(list, '.')); }
/// <summary> /// 获取格式化后内容 /// </summary> protected virtual string GetFormatContent() { string[] strs = new string[] { ConvertTool.ToString(this.AddTime), this.Type.ToString(), this.Position, this.Message, }; return(ConvertTool.ToString(strs, @" >> ") + "\n"); }
/// <summary> /// 获取创建时间最新的 N 条记录 /// </summary> /// <param name="info">文件夹目录信息</param> /// <param name="N">最新几条</param> /// <returns>完成排序的目录列表</returns> public static DirectoryInfo[] UpToDateDirectorys(DirectoryInfo info, int index, int N) { if (CheckData.IsObjectNull(info)) { return(new DirectoryInfo[] { }); } List <DirectoryInfo> sondir = new List <DirectoryInfo>(info.GetDirectories()); sondir.Sort((d1, d2) => d1.CreationTime == d2.CreationTime ? 0 :d1.CreationTime > d2.CreationTime ? -1 : 1); return(ConvertTool.ToRangePage(sondir, index, N)); }
/// <summary> /// 获取中文随机字符串 /// </summary> /// <param name="length">长度</param> public static string GetChineseString(int sum_length = 5) { StringBuilder result_str = new StringBuilder(); for (int i = 0; i < sum_length; i++) { int hexa_int_min_sign = CommonData.Unicode_Chinese_MIN_Decimal(); int hexa_int_max_sign = CommonData.Unicode_Chinese_MAX_Decimal(); int random_value = R.Next(hexa_int_min_sign, hexa_int_max_sign + 1); string random_heax_string = ConvertTool.DecimalToHexadecimal(random_value); string unicode_format_str = ConvertTool.UnicodeFormatString(random_heax_string); string chinese_char = ConvertTool.UnicodeToGB2312(unicode_format_str); result_str.Append(chinese_char); } return(result_str.ToString()); }
/// <summary> /// 格式化日志信息, 其中判断使用堆栈跟踪 /// </summary> /// <returns>格式化后文本内容</returns> protected override string GetFormatContent() { if (!this.IsStackTraceList) { return(base.GetFormatContent()); } string[] strs = new string[] { ConvertTool.ToString(this.AddTime), this.Type.ToString(), this.Message, }; string[] strs2 = new string[] { ConvertTool.ToString(strs, @" >> "), @"堆栈跟踪:", this.Position, }; return(ConvertTool.ToString(strs2, "\n") + "\n"); }
/// <summary> /// 只写文件内容 /// </summary> /// <param name="file_path">文件路径</param> /// <param name="content">写入内容</param> /// <param name="encoding">文件编码</param> public static void OnlyWrite(string file_path, string content, Encoding encoding) { string abs_file_path = PathHelp.ToAbsolute(file_path); if (CheckData.IsStringNull(abs_file_path)) { return; } content = ConvertTool.ToString(content); File.Delete(abs_file_path); PathHelp.CreateFileExists(abs_file_path); using (FileStream wfs = File.OpenWrite(abs_file_path)) { using (StreamWriter sw = new StreamWriter(wfs, encoding)) { sw.Write(content); sw.Flush(); } } }
/// <summary> /// 创建使用文件路径 /// </summary> /// <param name="directory">使用文件的文件夹路径</param> /// <param name="filename">文件名称</param> /// <returns>绝对路径的文件路径</returns> public static string CreateUseFilePath(string directory, string filename) { directory = ConvertTool.ToString(directory); if (CheckData.IsStringNull(directory)) { directory = @"/"; } filename = ConvertTool.ToString(filename); if (CheckData.IsStringNull(filename)) { return(string.Empty); } string abs_directory = ToAbsolute(directory).TrimEnd('\\'); if (!Directory.Exists(abs_directory)) { Directory.CreateDirectory(abs_directory); } string abs_filename = FilterDisableFileNameChar(filename); return(string.Format("{0}\\{1}", abs_directory, abs_filename)); }
/// <summary> /// 查找指定的 Attribute '特性' 内容列表 /// </summary> /// <typeparam name="A">指定的 Attribute '特性'</typeparam> /// <param name="memberInfo">元数据</param> /// <param name="isFindInherit">是否查找继承链, 默认不查找</param> /// <returns>返回指定'特性'类型列表结果</returns> public static A[] AttributeFindALL <A>(MemberInfo memberInfo, bool isFindInherit = false) where A : System.Attribute { object[] attrs = memberInfo.GetCustomAttributes(typeof(A), isFindInherit); return(ConvertTool.ListConvertType(attrs, o => o as A)); }
/// <summary> /// 过滤禁用文件名称路径错误字符 /// </summary> public static string FilterDisableFileNameChar(string filepath) { return(ConvertTool.FilterDisableChars(filepath, Path.GetInvalidFileNameChars())); }
/// <summary> /// 过滤禁用路径错误字符 /// </summary> public static string FilterDisablePathChar(string path) { return(ConvertTool.FilterDisableChars(path, Path.GetInvalidPathChars())); }
public ExplainAttribute(string explaninStr) { this._text = ConvertTool.ToString(explaninStr); }
/// <summary> /// 清除字符串前后的相对路径符号: '/' /// </summary> public static string ToPathSymbol(string sv) { return(ConvertTool.ToTrim(sv).Trim('/')); }
/// <summary> /// Unicode 中文字符集 最大 十进制标识 /// </summary> /// <returns></returns> public static int Unicode_Chinese_MAX_Decimal() { return(ConvertTool.HexadecimalToDecimal(Unicode_Chinese_MAX_Hexadecimal())); }
/// <summary> /// 获得模板字符串,从设置中的模板路径来读取模板文件. /// </summary> /// <param name="sitePath">站点目录</param> /// <param name="tempPath">模板目录</param> /// <param name="skinName">模板名</param> /// <param name="templateName">模板文件的文件名称</param> /// <param name="fromPage">源页面名称</param> /// <param name="inherit">该页面继承的类</param> /// <param name="buildPath">生成目录名</param> /// <param name="channelName">频道名称</param> /// <param name="nest">嵌套次数</param> /// <returns>string值,如果失败则为"",成功则为模板内容的string</returns> public static string GetTemplate(string sitePath, string tempPath, string skinName, string templet, string fromPage, string inherit, string buildPath, string channelName, string pageSize, int nest) { StringBuilder strReturn = new StringBuilder(220000); //返回的字符 string templetFullPath = Utils.GetMapPath(string.Format("{0}{1}/{2}/{3}", sitePath, tempPath, skinName, templet)); //取得模板文件物理路径 //超过5次嵌套退出 if (nest < 1) { nest = 1; } else if (nest > 5) { return(""); } //检查模板文件是否存在 if (!File.Exists(templetFullPath)) { return(""); } //开始读写文件 using (StreamReader objReader = new StreamReader(templetFullPath, Encoding.UTF8)) { StringBuilder extNamespace = new StringBuilder(); //命名空间标签转换容器 StringBuilder textOutput = new StringBuilder(70000); textOutput.Append(objReader.ReadToEnd()); objReader.Close(); //替换Csharp标签 foreach (Match m in r[3].Matches(textOutput.ToString())) { textOutput.Replace(m.Groups[0].ToString(), m.Groups[0].ToString().Replace("\r\n", "\r\t\r")); } //替换命名空间标签 foreach (Match m in r[2].Matches(textOutput.ToString())) { extNamespace.Append("\r\n<%@ Import namespace=\"" + m.Groups[1] + "\" %>"); textOutput.Replace(m.Groups[0].ToString(), string.Empty); } //替换特殊标记 textOutput.Replace("\r\n", "\r\r\r"); textOutput.Replace("<%", "\r\r\n<%"); textOutput.Replace("%>", "%>\r\r\n"); textOutput.Replace("<%csharp%>\r\r\n", "<%csharp%>").Replace("\r\r\n<%/csharp%>", "<%/csharp%>"); //开始查找替换标签 string[] strlist = Utils.SplitString(textOutput.ToString(), "\r\r\n"); for (int i = 0; i < strlist.Length; i++) { if (strlist[i] == "") { continue; } strReturn.Append(ConvertTags(nest, channelName, pageSize, sitePath, tempPath, skinName, strlist[i])); //搜索替换标签 } //如果是第一层则写入文件 if (nest == 1) { //定义页面常量 string channelStr = string.Empty; //频道名称 string constStr = string.Empty; //分页大小 if (channelName != string.Empty) { channelStr = "base.channel = \"" + channelName + "\";\r\n\t"; } if (pageSize != string.Empty && ConvertTool.ToInt(pageSize, 0) > 0) { constStr = "\r\n\tconst int pagesize = " + pageSize + ";"; } //页面头部声明 string template = string.Format("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" Inherits=\"{0}\" ValidateRequest=\"false\" %>\r\n" + "<%@ Import namespace=\"System.Collections.Generic\" %>\r\n" + "<%@ Import namespace=\"System.Text\" %>\r\n" + "<%@ Import namespace=\"System.Data\" %>\r\n" + "<%@ Import namespace=\"DTcms.Common\" %>{1}\r\n\r\n" + "<script runat=\"server\">\r\noverride protected void OnInit(EventArgs e)\r\n" + "{{\r\n\r\n\t/* \r\n\t\tThis page was created by DTcms Template Engine at {2}.\r\n\t\t" + "本页面代码由DTcms模板引擎生成于 {2}. \r\n\t*/\r\n\r\n\t{3}base.OnInit(e);\r\n\t" + "StringBuilder templateBuilder = new StringBuilder({4});{5}\r\n{6}\r\n\t" + "Response.Write(PrivateStringRule(templateBuilder.ToString()));\r\n}}\r\n</script>\r\n", inherit, extNamespace.ToString(), DateTime.Now, channelStr, strReturn.Capacity, constStr, Regex.Replace(strReturn.ToString(), @"\r\n\s*templateBuilder\.Append\(""""\);", "")); string pageDir = Utils.GetMapPath(string.Format("{0}aspx/{1}/", sitePath, buildPath)); //生成文件的目录路径 string outputPath = pageDir + fromPage; //生成文件的物理路径 //如果物理路径不存在则创建 if (!Directory.Exists(pageDir)) { Directory.CreateDirectory(pageDir); } //保存写入文件 File.WriteAllText(outputPath, template, Encoding.UTF8); //using (FileStream fs = new FileStream(outputPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) //{ // Byte[] info = Encoding.UTF8.GetBytes(template); // fs.Write(info, 0, info.Length); // fs.Close(); //} } } return(strReturn.ToString()); }
/// <summary> /// 获得指定表单参数的float类型值 /// </summary> /// <param name="strName">表单参数</param> /// <param name="defValue">缺省值</param> /// <returns>表单参数的float类型值</returns> public static float GetFormFloat(string strName, float defValue) { return(ConvertTool.ToFloat(HttpContext.Current.Request.Form[strName], defValue)); }
/// <summary> /// 获得指定表单参数的decimal类型值 /// </summary> /// <param name="strName">表单参数</param> /// <param name="defValue">缺省值</param> /// <returns>表单参数的decimal类型值</returns> public static decimal GetFormDecimal(string strName, decimal defValue) { return(ConvertTool.ToDecimal(HttpContext.Current.Request.Form[strName], defValue)); }
/// <summary> /// 获得指定表单参数的int类型值 /// </summary> /// <param name="strName">表单参数</param> /// <param name="defValue">缺省值</param> /// <returns>表单参数的int类型值</returns> public static int GetFormInt(string strName, int defValue) { return(ConvertTool.ToInt(HttpContext.Current.Request.Form[strName], defValue)); }
/// <summary> /// 获得指定Url参数的int类型值 /// </summary> /// <param name="strName">Url参数</param> /// <returns>Url参数的int类型值</returns> public static int GetQueryInt(string strName) { return(ConvertTool.ToInt(HttpContext.Current.Request.QueryString[strName], 0)); }