Skip to content

Instantly share code, notes, and snippets.

@yyamasak
Created March 17, 2015 11:17
Show Gist options
  • Save yyamasak/445eb591a773ee4eefd6 to your computer and use it in GitHub Desktop.
Save yyamasak/445eb591a773ee4eefd6 to your computer and use it in GitHub Desktop.
GetPrivateProfileSectionNames.exe .\system.ini
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.IO;
namespace TestGetPrivateProfileSectionNames
{
public class Program
{
[DllImport("kernel32.dll")]
static extern int GetPrivateProfileSectionNames(
IntPtr lpszReturnBuffer,
uint nSize,
string lpFileName);
public static void Main(string[] args)
{
string path = "";
if (args.Length > 0)
{
path = args[0];
}
else
{
path = @".\system.ini";
}
if (File.Exists(path))
{
Console.WriteLine("INI file exists. {0}", path);
}
else
{
Console.WriteLine("INI file missing. {0}", path);
}
IntPtr ptr = Marshal.StringToHGlobalAnsi(new String('\0', 1024));
int length = GetPrivateProfileSectionNames(ptr, 1024, path);
if (0 < length)
{
string result = Marshal.PtrToStringAnsi(ptr, length);
Array.ForEach<string>(result.Split('\0'), s => Console.WriteLine(s));
}
Marshal.FreeHGlobal(ptr);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment