您提供的信息【C似乎不完整,无法确定您想要表达的具体内容或问题。如果您能提供更多的上下文或详细信息,我将能够更好地帮助您。请告诉我您需要帮助的具体问题或信息。

2026-04-11 23:071阅读0评论SEO资源
  • 内容介绍
  • 相关推荐

本文共计671个文字,预计阅读时间需要3分钟。

您提供的信息【C似乎不完整,无法确定您想要表达的具体内容或问题。如果您能提供更多的上下文或详细信息,我将能够更好地帮助您。请告诉我您需要帮助的具体问题或信息。

一、前言在以往的项目中,通常会将一些用户可设置的值保存到XML文件中。然而,在最新的项目中,项目组更倾向于使用INI文件。INI文件虽小,但也需要一定的技巧才能有效地读取。下面将介绍如何读取INI文件。


一、前言

在以前的项目中一般会把一下用户可以设置是值放到xml文件中,但是最近的项目中,项目组长说要用ini文件。ini文件小编也接触过,但是对他的读取还是有一定的挑战的。下面就对ini文件进行读取。

您提供的信息【C似乎不完整,无法确定您想要表达的具体内容或问题。如果您能提供更多的上下文或详细信息,我将能够更好地帮助您。请告诉我您需要帮助的具体问题或信息。

二、ini文件介绍

通常C#使用基于XML的配置文件,不过如果有需要的话,比如要兼顾较老的系统,可能还是要用到INI文件。

但C#本身并不具备读写INI文件的API,只有通过调用非托管代码的方式,即系统自身的API才能达到所需的目的。

格式

节(section)

节用方括号括起来,单独占一行,例如: [section]

键(key)

键(key)又名属性(property),单独占一行用等号连接键名和键值,例如: name=value

注释(comment)

注释使用英文分号(;)开头,单独占一行。在分号后面的文字,直到该行结尾都全部为注释,例如: ; comment text

[host] # 设置服务器的ip- 王雷-2017年4月10日11:27:17 ip=222.168.33.117 [ports] # 设置服务器端口-王雷-2017年4月10日11:27:31 port=8080

三、读取方法

在读取之前呢,需要我们引入API:

using System; using System.Runtime.InteropServices; using System.Text; namespace INIDemo { class Program { static void Main(string[] args) { WritePrivateProfileString("Demo", "abc", "123", "c:\\demo.ini"); StringBuilder temp = new StringBuilder(); GetPrivateProfileString("Demo", "abc", "", temp, 255, "c:\\demo.ini"); Console.WriteLine(temp); Console.ReadLine(); } [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern bool WritePrivateProfileString( string lpAppName, string lpKeyName, string lpString, string lpFileName); [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern int GetPrivateProfileString( string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); } }

把方法封装,调用就可以:

#region 读Ini文件 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文件 public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath) { if (File.Exists(iniFilePath)) { bool OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath); if (OpStation) { return true; } else { return false; } } else { return false; } } #endregion

四、小结

通过这次的学习,对配置文件了解更加深刻了,读取中也有了深刻的认识。键值对的ini配置文件也是很不错的。


本文共计671个文字,预计阅读时间需要3分钟。

您提供的信息【C似乎不完整,无法确定您想要表达的具体内容或问题。如果您能提供更多的上下文或详细信息,我将能够更好地帮助您。请告诉我您需要帮助的具体问题或信息。

一、前言在以往的项目中,通常会将一些用户可设置的值保存到XML文件中。然而,在最新的项目中,项目组更倾向于使用INI文件。INI文件虽小,但也需要一定的技巧才能有效地读取。下面将介绍如何读取INI文件。


一、前言

在以前的项目中一般会把一下用户可以设置是值放到xml文件中,但是最近的项目中,项目组长说要用ini文件。ini文件小编也接触过,但是对他的读取还是有一定的挑战的。下面就对ini文件进行读取。

您提供的信息【C似乎不完整,无法确定您想要表达的具体内容或问题。如果您能提供更多的上下文或详细信息,我将能够更好地帮助您。请告诉我您需要帮助的具体问题或信息。

二、ini文件介绍

通常C#使用基于XML的配置文件,不过如果有需要的话,比如要兼顾较老的系统,可能还是要用到INI文件。

但C#本身并不具备读写INI文件的API,只有通过调用非托管代码的方式,即系统自身的API才能达到所需的目的。

格式

节(section)

节用方括号括起来,单独占一行,例如: [section]

键(key)

键(key)又名属性(property),单独占一行用等号连接键名和键值,例如: name=value

注释(comment)

注释使用英文分号(;)开头,单独占一行。在分号后面的文字,直到该行结尾都全部为注释,例如: ; comment text

[host] # 设置服务器的ip- 王雷-2017年4月10日11:27:17 ip=222.168.33.117 [ports] # 设置服务器端口-王雷-2017年4月10日11:27:31 port=8080

三、读取方法

在读取之前呢,需要我们引入API:

using System; using System.Runtime.InteropServices; using System.Text; namespace INIDemo { class Program { static void Main(string[] args) { WritePrivateProfileString("Demo", "abc", "123", "c:\\demo.ini"); StringBuilder temp = new StringBuilder(); GetPrivateProfileString("Demo", "abc", "", temp, 255, "c:\\demo.ini"); Console.WriteLine(temp); Console.ReadLine(); } [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern bool WritePrivateProfileString( string lpAppName, string lpKeyName, string lpString, string lpFileName); [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] private static extern int GetPrivateProfileString( string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); } }

把方法封装,调用就可以:

#region 读Ini文件 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文件 public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath) { if (File.Exists(iniFilePath)) { bool OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath); if (OpStation) { return true; } else { return false; } } else { return false; } } #endregion

四、小结

通过这次的学习,对配置文件了解更加深刻了,读取中也有了深刻的认识。键值对的ini配置文件也是很不错的。