如果您正在嘗試讀取ini文件以獲取值,則可以使用該文件。 IniFile.ReadIniValue(「[Tag]」,「Server」,@「C:\ my.ini」);
#region Usings
using System.Text;
using System.Runtime.InteropServices;
#endregion
/// <summary>
/// Communicates with ini files
/// </summary>
public static class IniFile
{
#region Declarations
#endregion
#region Constructor/Deconstructor
/// <summary>
/// Initializes a new instance of the <see cref="IniFile"/> class.
/// </summary>
static IniFile()
{
}
#endregion
#region Properties
#endregion
#region Win32_API
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(
string section,
string key, string def,
StringBuilder retVal,
int size, string filePath);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool WritePrivateProfileString(string lpAppName,
string lpKeyName, string lpString, string lpFileName);
#endregion
/// <summary>
/// Reads the ini value.
/// </summary>
/// <param name="section">The section.</param>
/// <param name="key">The key.</param>
/// <param name="iniFilePath">The ini file path.</param>
/// <returns>Value stored in key</returns>
/// <exception cref="FileNotFoundException"></exception>
public static string ReadIniValue(string section, string key, string iniFilePath)
{
if(!File.Exists(iniFilePath))
{
throw new FileNotFoundException();
}
const int size = 255;
var buffer = new StringBuilder(size);
var len = GetPrivateProfileString(section, key, string.Empty, buffer, size, iniFilePath);
if (len > 0)
{
return buffer.ToString();
}
return string.Empty;
}
/// <summary>
/// Writes the ini value.
/// </summary>
/// <param name="section">The section.</param>
/// <param name="keyname">The keyname.</param>
/// <param name="valueToWrite">The value to write.</param>
/// <param name="iniFilePath">The ini file path.</param>
/// <returns>true if write was successful, false otherwise</returns>
public static bool WriteIniValue(string section,string keyname,string valueToWrite,string iniFilePath)
{
return WritePrivateProfileString(section, keyname, valueToWrite, iniFilePath);
}
}
謝謝,我會試試看。 – kyork 2011-03-25 13:26:39
我最終不得不硬編碼ini文件的路徑,我不喜歡,但其餘的現在工作。 – kyork 2011-03-25 21:58:34
根據您的ini文件的位置,您可以使用Environment.SpecialFolder使其更具動態性。 – Dylan 2011-03-25 22:07:42