Web Design that taps into the haromny and vision of your dreams.

Reading and Writing INI Files with C#

By on in Coding

123 words, estimated reading time 1 minutes.

This short snippet is another tool in my utility class. You can use it to quickly read and write settings from an old school INI configuration file. Yes I still use INI files!

using System;
using System.Runtime.InteropServices;
using System.Text;
public static class IniFile
  private static extern long WritePrivateProfileString(string section,string key, string val, string filePath);
  private static extern int GetPrivateProfileString(string section,string key, string def, StringBuilder retVal,int size, string filePath);
  public static void WriteValue(string path, string Section, string Key, string Value)
    WritePrivateProfileString(Section, Key, Value, path);
  public static string ReadValue(string path, string Section, string Key)
    StringBuilder temp = new StringBuilder(255);
    int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
    return temp.ToString();

Usage is very simple, simply call IniFile.WriteValue or IniFile.ReadValue with the full path to the INI file, the section name and the configuration key.

Last updated on: Wednesday 21st June 2017



Have a question or suggestion? Please leave a comment to start the discussion.


Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.