公共方法

来源:互联网 发布:origin软件中文版 编辑:程序博客网 时间:2024/04/25 13:50
1, dataTable过滤的行组成新的dataTable
        // 把行集合中的数据覆盖到datatable        public static DataTable GetDataTable(DataTable dtOrigin, DataRow[] DRS)        {            DataTable dtTmp = dtOrigin.Clone();            try            {                if (DRS != null && DRS.Length > 0)                {                    foreach (DataRow row in DRS)                    {                        dtTmp.Rows.Add(row.ItemArray);                    }                }            }            catch (Exception ex)            {                //MessageBox.Show(ex.Message);            }            return dtTmp;        }

 2, 判断数组中是否包含某个项 

        public static bool ArrayHasItem(string[] arr, string sItem)        {            bool bFind = false;            foreach (string s in arr)            {                if (s == sItem)                {                    bFind = true;                    break;                }            }            return bFind;        }

3, 删除数组/泛型数组中重复的项

        /// <summary>        /// 删除数组中的重复项 泛型方法        /// </summary>        /// <param name="values">数组</param>        /// <returns></returns>        public static List<T> RemoveDuplicate<T>(List<T> values)        {            List<T> list = new List<T>();            for (int i = 0; i < values.Count; i++)//遍历数组成员            {                if (list.IndexOf(values[i]) == -1)//对每个成员做一次新数组查询如果没有相等的则加到新数组                    list.Add(values[i]);            }            return list;        }
调用方法:

  List<string> aWordNew = new List<string>();

  // -----

   aWordNew = RemoveDuplicate<string>(aWordNew);

4,创建DataTable

       public static DataTable CreateDataTable(int iColumnCount)        {            DataTable dt = new DataTable();            for (int i = 0; i < iColumnCount; i++)            {                dt.Columns.Add("Column" + i);            }            return dt;        }
5,设置 / 读取 配置文件中某个节点的值

       public static void SetAppSettingsKeyValue(string AppKey, string AppValue)        {            XmlDocument xDoc = new XmlDocument();            xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");            XmlNode xNode;            XmlElement xElem1;            XmlElement xElem2;            xNode = xDoc.SelectSingleNode("//appSettings");            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");            if (xElem1 != null) xElem1.SetAttribute("value", AppValue);            else            {                xElem2 = xDoc.CreateElement("add");                xElem2.SetAttribute("key", AppKey);                xElem2.SetAttribute("value", AppValue);                xNode.AppendChild(xElem2);            }            xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");        }       public static string GetAppValue(string sKeyName)        {            object oResult = System.Configuration.ConfigurationManager.AppSettings[sKeyName];            if (oResult!=null)            {                 return oResult.ToString();            }            return "";        }

配置文件如下:

<?xml version="1.0"?><configuration><startup><supportedRuntime version="v2.0.50727"/></startup>  <appSettings>    <add key="SqliteDbPath" value="Data\EnglishWords.db"/>    <add key="TestSourcePath" value="Data\Dictionary_English.docx"/>    <add key="RunMode" value="DEV"/> <!--运行模式:DEV,USER -->  </appSettings></configuration>

6,某对象是null或空

        public static bool IsNullOrEmptyObject(object oSource)        {            if (oSource != null)            {                return string.IsNullOrEmpty(oSource.ToString());            }            return true;        }
7,检查字符串是不是包含中文字符
      检查字符串是不是包含中文字符        public static bool CheckEncode(string srcString)        {            int strLen = srcString.Length;            //字符串的长度,一个字母和汉字都算一个             int bytLeng = System.Text.Encoding.UTF8.GetBytes(srcString).Length;            //字符串的字节数,字母占1位,汉字占2位,注意,一定要UTF8             bool chkResult = false;            if (strLen < bytLeng)  //如果字符串的长度比字符串的字节数小,当然就是其中有汉字啦^-^             {                chkResult = true;            }            return chkResult;        }
8, 提示信息

    public static bool DeleteConfirmation()        {            return MessageBox.Show("确定要删除吗?", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)               == DialogResult.OK;        }        public static bool DeleteConfirmation(string sItem)        {            return MessageBox.Show("确定要删除 " + sItem + " 吗?", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)               == DialogResult.OK;        }        public static bool Confirmation(string sMsg)        {            return MessageBox.Show(sMsg, "确认信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)               == DialogResult.OK;        }        public static bool SingleConfirmation(string sMsg)        {            return MessageBox.Show(sMsg, "确认信息", MessageBoxButtons.OK, MessageBoxIcon.Question)               == DialogResult.OK;        }