常用的.net公用类方法
来源:互联网 发布:淘宝店标图片清新 编辑:程序博客网 时间:2024/05/21 14:08
/// <summary>
/// 浏览某一目录下的所有文件信息并返回相关信息的数组
/// </summary>
/// <param name="str">c:\test\test(路径)</param>
/// <returns>0文件名,1创建时间,2扩展名,3文件大小</returns>
public static string[] GetFileInfo(string str)
{
str = HttpUtility.UrlDecode(str);
string[] temp = new string[4];
DirectoryInfo dir = new DirectoryInfo(str);
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
temp[0] += file.Name.ToString() + "|"; //文件名
temp[1] += file.CreationTime.ToString() + "|"; //文件日期
temp[2] += file.Extension.ToString() + "|";//扩展名
temp[3] += file.Length + "|";//文件长度
}
return temp;
}
/// <summary>
/// 过滤非法字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string filtRiskChar(string str) //过滤非法字符
{
string s = "";
s = str.Replace("'", " ");
s = s.Replace(";", " ");
s = s.Replace("1=1", " ");
s = s.Replace("|", " ");
s = s.Replace("<", " ");
s = s.Replace(">", " ");
return s;
}
/// <summary>
/// 对实体类进行排序
/// </summary>
/// <typeparam name="T">实体类型,如:User</typeparam>
/// <param name="list">实体类的数组</param>
/// <param name="order">排序字段(必须为属性)</param>
/// <param name="asc">是否按正序排序</param>
public static void Sort<T>(object[] list, string order, bool asc)
{
Type type = typeof(T);
PropertyInfo[] pros = type.GetProperties();
PropertyInfo pro = pros[0];
order = order.ToLower();
for (int i = 0; i < pros.Length; i++)
{
if (pros[i].Name.ToLower().Equals(order))
{
pro = pros[i];
break;
}
}
object obj;
int j, k = 1;
bool done = false;
int len = list.Length;
while (k < len && !done)
{
done = true;
for (j = 0; j < len - k; j++)
{
int b = pro.GetValue(list[j], null).ToString().CompareTo(pro.GetValue(list[j + 1], null).ToString());
if ((asc && b > 0) || (!asc && b < 0))
{
done = false;
obj = list[j];
list[j] = list[j + 1];
list[j + 1] = obj;
}
}
k++;
}
}
/// <summary>
/// 取得绝对路径
/// </summary>
/// <param name="strPath"></param>
/// <returns></returns>
public static string MapPath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("~"))
{
strPath = strPath.Remove(0, 1);
}
if (strPath.StartsWith("\\"))
{
strPath = strPath.TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}
/// <summary>
/// 读出模版
/// </summary>
/// <param name="MBPath">模版路径</param>
/// <returns></returns>
public static StringBuilder getMB(string MBPath)
{
string mbPath = MapPath(MBPath);
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StringBuilder sb = new StringBuilder("");
try
{
sr = new StreamReader(mbPath, code);
string str = sr.ReadToEnd();
sb.Append(str);
sr.Close();
return sb;
}
catch
{
return null;
}
finally
{
}
}
- 常用的.net公用类方法
- IOS 常用方法 公用集合类
- 常用js代码公用方法
- iOS开发---常用公用方法
- 四舍五入的公用方法
- Object类公用方法
- hibernateDaoSupport中的一些公用常用方法
- hibernateDaoSupport中的一些公用常用方法
- 我的框架:公用方法
- Object的一些公用方法
- 公用方法的一些转换
- 【java】Object的公用方法
- 类的公用继承
- 多个非常有用的Java程序片段-常用java示例-常用示例-范例-公共方法-公用方法
- 2.多个非常有用的Java程序片段-常用java示例-常用示例-范例-公共方法-公用方法-2
- asp.net: DropDownList绑定数据源及选中某个值的公用方法及例子
- 。net中一些常用的类方法
- asp.net2005(c#) Treeview的公用方法
- nand的几个名词 oob bbt ecc
- CVF转换到IVF的bug调试
- android截屏代码
- Linux系统启动时间优化方案
- 两年总结
- 常用的.net公用类方法
- [iPhone中级] iPhone团购信息客户端的开发 (一)
- 常用的优秀javascript资源汇总(转)
- Lua语言介绍
- [RTSP][2012-07-4] typedef
- java 枚举高级应用之状态机
- HDU 1078 FatMouse and Cheese(记忆搜索)
- 关于QT+OPENCV 实现人脸检测
- 盖茨之梦--“surface产品--平板电脑和PC的组合”