码迷,mamicode.com
首页 > 其他好文 > 详细

常用的一些操作方法【1】

时间:2018-06-03 17:33:46      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:sre   hide   rtc   reac   star   写文件   none   builder   get   

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
View Code

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);
                        }

                    }
View Code

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

 

常用的一些操作方法【1】

标签:sre   hide   rtc   reac   star   写文件   none   builder   get   

原文地址:https://www.cnblogs.com/yanbigfeg/p/9129381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!