常用的一些操作方法
Posted 深井田蛙
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用的一些操作方法相关的知识,希望对你有一定的参考价值。
1.反射创建类型
//3 列排序 Type t = data.ElementType; //var pcs = Activator.CreateInstance(t, new object[] { // "addTime", // Common.ListSortDirection.Ascending //}); Type type = typeof(PropertySortCondition<>); type = type.MakeGenericType(t); var pcs = Activator.CreateInstance(type, new object[] { "addTime", Common.ListSortDirection.Ascending });
2.写入TXT文件
#region 写文件(.txt-添加) /// <summary> /// 写文件 /// </summary> /// <param name="FileName">文件路径,相对路径,不需要写扩展名</param> /// <param name="Strings">文件内容</param> private void WriteFile(string FileName, string Strings) { string Path = System.Web.HttpContext.Current.Server.MapPath("~/" + FileName + ".txt"); if (!System.IO.File.Exists(Path)) { System.IO.FileStream f = System.IO.File.Create(Path); f.Close(); f.Dispose(); } System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8); f2.WriteLine(Strings); f2.Close(); f2.Dispose(); } #endregion
3.参数对比与赋值
System.Threading.Interlocked.CompareExchange(ref "参数1","参数3","参数2");
参数1与参数2对比,一样就把参数3复制给参数1.不一样就把参数2赋值给参数1.这个方法返回的是原始的参数1,即返回值在此方法没有赋值前已经确认。
4.操作xml文件
//创建xml文件 string mPath = Application.StartupPath + "/record.xml";//创建位置 if (!File.Exists(mPath)) { CreateXMLDoc(); mXmlDoc.Load(mPath); } //添加节点 public void CreateXMLDoc() { if (!File.Exists(mPath)) { XmlDocument xmldoc = new XmlDocument(); //加入XML的声明段落 xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null)); XmlElement rMainNode = xmldoc.CreateElement("", "settings", ""); rMainNode.IsEmpty = false; xmldoc.AppendChild(rMainNode); XmlElement rIp = xmldoc.CreateElement("", "ips", ""); rIp.IsEmpty = false; rMainNode.AppendChild(rIp); XmlElement rPort = xmldoc.CreateElement("", "ports", ""); rPort.IsEmpty = false; rMainNode.AppendChild(rPort); XmlElement rName = xmldoc.CreateElement("", "names", ""); rName.IsEmpty = false; rMainNode.AppendChild(rName); XmlElement rPreviousRecord = xmldoc.CreateElement("", "previousreocrd", ""); rPreviousRecord.IsEmpty = false; rMainNode.AppendChild(rPreviousRecord); xmldoc.Save(mPath); } } /////删除节点 XmlNode rMainNode = mXmlDoc.SelectSingleNode("settings"); XmlNode rNode = rMainNode.SelectSingleNode("names"); XmlNodeList rList = rNode.ChildNodes; List<string> list = new List<string>(); foreach (XmlNode r in rList) { XmlElement rElem = (XmlElement)r; list.Add(rElem.GetAttribute("value").ToString().Split(‘,‘)[0]); if (rElem.GetAttribute("value").ToString().Split(‘,‘)[0] == tb_name.Text) { rNode.RemoveChild(rElem); } }
5.读写ini文件
#region 读Ini文件 /// <summary> /// /// </summary> /// <param name="Section">节名</param> /// <param name="Key">键名</param> /// <param name="NoText">读取失败返回值</param> /// <param name="iniFilePath">路径</param> /// <returns>读取值</returns> public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath) { if (File.Exists(iniFilePath)) { StringBuilder temp = new StringBuilder(1024); GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath); return temp.ToString(); } else { return String.Empty; } } #endregion #region 写Ini文件 /// <summary> /// /// </summary> /// <param name="Section">节名</param> /// <param name="Key">键名</param> /// <param name="Value">值</param> /// <param name="iniFilePath">路径</param> /// <returns></returns> public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath) { if (File.Exists(iniFilePath)) { long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath); if (OpStation == 0) { return false; } else { return true; } } else { return false; } } #endregion
以上是关于常用的一些操作方法的主要内容,如果未能解决你的问题,请参考以下文章