using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Collections; using System.Configuration; using System.Xml; namespace SSLMS.Common { public class StringUtil { /// /// 获得日期 /// /// public static string GetCurDateTimeString() { return System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } /// /// 获得GUID /// /// public static string GetGUID() { return System.Guid.NewGuid().ToString(); } /// /// 获得计算机名 /// /// public static string GetHostName() { return System.Net.Dns.GetHostName().ToString(); } /// /// 获得分离字符串中的某组字符 /// /// /// /// /// public static string GetSplitStr(string ControlName, char[] ch, int index) { string[] arrTmp = ControlName.Split(ch); return arrTmp[(1 == arrTmp.Length) ? 0 : index]; } public static string strFormat(string str, string[] strPara) { return string.Format(str, strPara); } public static DataTable AddAll(DataTable dt, Hashtable ht) { if (0 == ht.Count) { ht.Add("name", "-"); ht.Add("value", ""); } DataTable dtResult = dt; if (null == dtResult) { dtResult = new DataTable(); foreach (DictionaryEntry d in ht) { dtResult.Columns.Add(d.Key.ToString()); } } if (0 == dtResult.Columns.Count) { dtResult = new DataTable(); foreach (DictionaryEntry d in ht) { dtResult.Columns.Add(d.Key.ToString()); } } DataRow dr = dtResult.NewRow(); foreach (DictionaryEntry d in ht) { dr[d.Key.ToString()] = d.Value; } dtResult.Rows.InsertAt(dr, 0); return dtResult; } public static string ConfigGet(string str) { string sResult = str; sResult = new AppSettingsReader().GetValue(str, typeof(string)).ToString().Trim(); return sResult; } /// /// 设置配置文件 /// /// public static bool ConfigSet(string sFileName, string sKey, string sValue, out string sResult) { bool bResult = true; sResult = string.Empty; try { string sPath = string.Empty; Assembly Asm = Assembly.GetExecutingAssembly(); XmlDocument xmlDoc = new XmlDocument(); sPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + sFileName; xmlDoc.Load(sPath); XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes; foreach (XmlNode xn in nodeList)//遍历所有子节点 { try { XmlElement xe = (XmlElement)xn; if (xe.GetAttribute("key").IndexOf(sKey) != -1) { xe.SetAttribute("value", sValue); } } catch { } } xmlDoc.Save(sPath); } catch (Exception ex) { bResult = false; sResult = ex.Message; } return bResult; } /// /// IList转DataTable /// /// /// public static DataTable ListToDataTable(IList list) { DataTable dtResult = new DataTable(); if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { dtResult.Columns.Add(pi.Name, pi.PropertyType); } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } object[] array = tempList.ToArray(); dtResult.LoadDataRow(array, true); } } return dtResult; } /// ///DataTable转IList /// /// /// /// public static IList DataTableToList(DataTable dt, Hashtable ht) { IList lsResult = new List(); DataTable dtTmp = dt.Clone(); PropertyInfo[] propertys = typeof(T).GetProperties(); foreach (PropertyInfo pi in propertys) { try { dtTmp.Columns[pi.Name].DataType = pi.PropertyType; } catch { } } foreach (DataRow dr in dt.Rows) { dtTmp.Rows.Add(dr.ItemArray); T t = Activator.CreateInstance(); string sKey = string.Empty; foreach (PropertyInfo pi in propertys) { sKey = pi.Name; try { if (ht.ContainsKey(sKey)) { pi.SetValue(t, dtTmp.Rows[dtTmp.Rows.Count - 1][ht[sKey].ToString()], null); } else { pi.SetValue(t, dtTmp.Rows[dtTmp.Rows.Count - 1][sKey], null); } } catch { } } lsResult.Add(t); } return lsResult; } /// ///IList转IList /// /// /// /// public static IList ListToList(IList lsIn, Hashtable ht) { IList lsResult = new List(); //DataTable dtTmp = dt.Clone(); //PropertyInfo[] propertys = typeof(T).GetProperties(); //foreach (PropertyInfo pi in propertys) //{ // try // { // dtTmp.Columns[pi.Name].DataType = pi.PropertyType; // } // catch // { // } //} //foreach (DataRow dr in dt.Rows) //{ // dtTmp.Rows.Add(dr.ItemArray); // T t = Activator.CreateInstance(); // string sKey = string.Empty; // foreach (PropertyInfo pi in propertys) // { // sKey = pi.Name; // try // { // if (ht.ContainsKey(sKey)) // { // pi.SetValue(t, dtTmp.Rows[dtTmp.Rows.Count - 1][ht[sKey].ToString()], null); // } // else // { // pi.SetValue(t, dtTmp.Rows[dtTmp.Rows.Count - 1][sKey], null); // } // } // catch // { // } // } // lsResult.Add(t); //} return lsResult; } } }