1.Ini是什么?(我对它的理解,用于存储用户配置信息的文件,该文件放在用户电脑...)
INI文件是一个无固定标准格式的配置文件。它以简单的文字与简单的结构组成,常常使用在Windows操作系统,或是其他操作系统上,许多程序也会采用INI文件做为设置程序之用。Windows操作系统后来以注册表的形式取代掉INI档。INI文件的命名来源,是取自英文“初始(Initial)”的首字缩写,正与它的用途——初始化程序相应。有时候,INI文件也会以不同的扩展名,如“.CFG”、“.CONF”、或是“.TXT”代替。
ps:
注释使用分號表示(;)。在分號後面的文字,直到該行結尾都全部為註解。
      /// <summary>
/// INI文件读取类
/// </summary>
public class INIHelper
{
/// <summary>
/// 文件名
/// </summary>
public static string str = "setting.ini";
/// <summary>
/// 文件路径
/// </summary>
public static string path = System.AppDomain.CurrentDomain.BaseDirectory + str; [DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); [DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath); /// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public static void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, path);
} /// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section">节点名称</param>
/// <param name="Key">键</param>
/// <param name="path">文件路径</param>
/// <returns></returns>
public static string IniReadValue(string Section, string Key, string path)
{
StringBuilder temp = new StringBuilder();
int i = GetPrivateProfileString(Section, Key, "", temp, , path);
return temp.ToString();
} /// <summary>
/// 读取INI文件
/// </summary>
/// <param name="section">节点名称</param>
/// <param name="key">键</param>
/// <returns></returns>
public static byte[] IniReadValues(string section, string key)
{
byte[] temp = new byte[];
int i = GetPrivateProfileString(section, key, "", temp, , path);
return temp; }
/// <summary>
/// 删除ini文件下所有节点
/// </summary>
public static void ClearAllSection()
{
IniWriteValue(null, null, null);
}
/// <summary>
/// 删除ini文件指点节点下的所有键
/// </summary>
/// <param name="Section"></param>
public static void ClearSection(string Section)
{
IniWriteValue(Section, null, null);
}
}
2.Json是什么?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming LanguageStandard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
目前,接触比较多都是Newtonsoft.Json,它封装的很齐全,记忆比较深刻的是,它用通过字段的属性,来确定字段是否被序列化以及null不序列化等功能,用来支撑变化多端的需求。
参考:Newtonsoft https://www.newtonsoft.com/json
  /// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T DeserializeObject<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
/// <summary>
/// 序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static string DeserializeObject<T>(object obj)
{
JsonConvert.SerializeObject(obj);
}
3.Xml是什么?(更接近人类的语言)
In computing, Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The W3C's XML 1.0 Specification[2] and several other related specifications[3]—all of them free open standards—define XML.[4]
The design goals of XML emphasize simplicity, generality, and usability across the Internet.[5] It is a textual data format with strong support via Unicode for different human languages. Although the design of XML focuses on documents, the language is widely used for the representation of arbitrary data structures[6] such as those used in web services.
Several schema systems exist to aid in the definition of XML-based languages, while programmers have developed many application programming interfaces (APIs) to aid the processing of XML data.
 public class AppConfigXmlHelper
{
public static string AppConfig()
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
string strDirectoryPath = Environment.CurrentDirectory + "\\IniJsonXmlTest.EXE.config";
return strDirectoryPath;
}
public static string GetPath(string key)
{
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(AppConfig());
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings");    //补充,需要在你的app.config 文件中增加一下,<appSetting> </appSetting>
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch (Exception)
{
return "";
}
}
public static void SetPath(string key, string path)
{
XmlElement xElem;
XmlDocument doc = new XmlDocument();
doc.Load(AppConfig());
XmlNode node = doc.SelectSingleNode(@"//appSettings");
xElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
xElem.SetAttribute("value", path);
doc.Save(AppConfig());
}
}
public class XmlSerialization
{ public static System.IO.Stream Serialize(object obj, Encoding encode, System.IO.Stream s)
{
TextWriter writer = null;
writer = new StreamWriter(s, encode); XmlSerializer ser = new XmlSerializer(obj.GetType()); ser.Serialize(writer, obj);
return s; } public static MemoryStream Serialize(object obj, Encoding encode)
{
MemoryStream s = new MemoryStream();
Serialize(obj, encode, s);
s.Position = ;
return s;
} public static MemoryStream Serialize(object obj, String encode)
{
return Serialize(obj, Encoding.GetEncoding(encode));
} public static MemoryStream Serialize(object obj)
{
return Serialize(obj, Encoding.UTF8);
} public static object Deserialize(System.IO.Stream In, Type objType)
{
In.Position = ;
XmlSerializer ser = new XmlSerializer(objType);
return ser.Deserialize(In);
}
} public class XmlSerialization<T>
{
public static System.IO.Stream Serialize(T obj, Encoding encode, System.IO.Stream s)
{
TextWriter writer = null;
writer = new StreamWriter(s, encode); XmlSerializer ser = new XmlSerializer(typeof(T)); ser.Serialize(writer, obj);
return s;
} public static MemoryStream Serialize(T obj, Encoding encode)
{
MemoryStream s = new MemoryStream();
Serialize(obj, encode, s);
s.Position = ;
return s;
} public static MemoryStream Serialize(T obj, String encode)
{
return Serialize(obj, Encoding.GetEncoding(encode));
} public static MemoryStream Serialize(T obj)
{
return Serialize(obj, Encoding.UTF8);
} public static T Deserialize(System.IO.Stream In)
{
In.Position = ;
XmlSerializer ser = new XmlSerializer(typeof(T));
return (T)ser.Deserialize(In);
}
}
4.序列化与反序列化
  • 序列化: 将数据结构或对象转换成二进制串的过程。
  • 反序列化:将在序列化过程中所生成的二进制串转换成数据结构或者对象的过程。
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
    public static class Serializer
{ #region 二进制
/// <summary>
/// 序列化二进制文件
/// </summary>
/// <param name="path"></param>
/// <param name="data"></param>
public static void SaveBinary(string path, object data)
{
if (File.Exists(path))
{
File.Delete(path);
}
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = File.Create(path);
bf.Serialize(fs, data);
fs.Close();
} /// <summary>
/// 反序列化二进制文件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path"></param>
/// <returns></returns>
public static T LoadBinary<T>(string path)
{
if (!File.Exists(path))
{
return default(T);
}
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = File.OpenRead(path);
object data = bf.Deserialize(fs);
fs.Close();
return (T)data;
}
#endregion #region xml
/// <summary>
/// 序列化XML文件
/// </summary>
/// <param name="path"></param>
/// <param name="data"></param>
/// <param name="type"></param>
public static void SaveXml(string path, object data, Type type)
{
if (File.Exists(path))
{
File.Delete(path);
}
XmlSerializer xs = new XmlSerializer(type);
FileStream fs = File.Create(path);
xs.Serialize(fs, data);
fs.Close();
} /// <summary>
/// 反序列化XML文件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path"></param>
/// <param name="type"></param>
/// <returns></returns>
public static T LoadXml<T>(string path, Type type)
{
if (!File.Exists(path))
{
return default(T);
}
XmlSerializer xs = new XmlSerializer(type);
FileStream fs = File.OpenRead(path);
object data = xs.Deserialize(fs);
fs.Close();
return (T)data;
}
#endregion #region json
/// <summary>
/// 序列化JSON
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static string Serialize(object data)
{
JavaScriptSerializer json = new JavaScriptSerializer();
json.MaxJsonLength = Int32.MaxValue;
return json.Serialize(data);
} /// <summary>
/// 反序列化JSON
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T Deserialize<T>(string json)
{
JavaScriptSerializer js = new JavaScriptSerializer();
js.MaxJsonLength = Int32.MaxValue;
return js.Deserialize<T>(json);
}
#endregion }
参考:

C# Ini、Json、Xml 封装类的更多相关文章

  1. c#通用配置文件读写类(xml,ini,json)

    .NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...

  2. c#通用配置文件读写类与格式转换(xml,ini,json)

    .NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...

  3. Python导出Excel为Lua/Json/Xml实例教程(三):终极需求

    相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 Python导出E ...

  4. Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验

    Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...

  5. Python导出Excel为Lua/Json/Xml实例教程(一):初识Python

    Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...

  6. JSON&XML总结

    JSON&XML: JSON----- //英译 Serialization:序列化 perform:执行 segue:继续 IOS5后 NSJSONSerialization解析 解析JSO ...

  7. JSON/XML序列化与反序列化(非构造自定义类)

    隔了很长时间再重看自己的代码,觉得好陌生..以后要养成多注释的好习惯..直接贴代码..对不起( ▼-▼ ) 保存保存:进行序列化后存入应用设置里 ApplicationDataContainer _a ...

  8. php返回json,xml,JSONP等格式的数据

    php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...

  9. Atitit.json xml 序列化循环引用解决方案json

    Atitit.json xml 序列化循环引用解决方案json 1. 循环引用1 2. 序列化循环引用解决方法1 2.1. 自定义序列化器1 2.2. 排除策略1 2.3. 设置序列化层次,一般3级别 ...

  10. 【转】[WCF REST] 帮助页面与自动消息格式(JSON/XML)选择

    可以说WebHttpBinding和WebHttpBehavior是整个Web HTTP编程模型最为核心的两个类型,前者主要解决消息编码问题,而余下的工作基本上落在了终结点行为WebHttpBehav ...

随机推荐

  1. 数模美赛准备——我的第一个LaTex文档

    软件下载地址:清华大学镜像网站(点击下载) https://mirrors.tuna.tsinghua.edu.cn/ctex/legacy/2.9/ 上述网站页面 根据个人需求下载不同的版本安装完成 ...

  2. AJAX的简单解释

    什么是AJAX AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以 ...

  3. 关于Python的一些看法

    我是一个学新闻传播的编程新人,对于电脑这一方面,我是真的有一些一窍不通啊~选择Python语言程序设计的原因也是因为出于Python对未来专业可能会起到帮助考虑(因为未来我的专业会涉及到一些大数据抽查 ...

  4. ASP.NET MVC Display Mode 移动端视图 配置对微信内置浏览器的识别

    最近在捣鼓一个稍微有点low的商城网站,没有计划做app却要求有个wap版,而前端又没有做成响应式,时间WTF,直接利用了asp.net mvc的Display Mode Provider. 使用方式 ...

  5. 学习创建spring boot

    一.什么是spring boot? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开 ...

  6. PDF 补丁丁 0.6.0.3427 版发布(修复提取图片问题)

    新的版本进一步改善了导出图片的问题.

  7. json对象转对象

    方式1:var obj = JSON.parse(jsonObject); 方式2:var obj = eval("("+jsonObject+")");

  8. aop表达式

    任意公共方法的执行: execution(public * *(..)) 任何一个以“set”开始的方法的执行: execution(* set*(..)) AccountService 接口的任意方 ...

  9. Floyd判断环算法总结

    Floyd判断环算法 全名Floyd’s cycle detection Algorithm, 又叫龟兔赛跑算法(Floyd's Tortoise and Hare),常用于链表.数组转化成链表的题目 ...

  10. 活代码LINQ——06

    一.模块代码 ' Fig. 9.4: LINQWithArrayOfObjects.vb ' LINQ to Objects using an array of Employee objects. M ...