yyamasak
3/17/2015 - 11:17 AM

GetPrivateProfileSectionNames.exe .\system.ini

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);
        }
    }
}