Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Save INI files with ease.

Hey, here is a fixed INI lib code for your projects. Save INI files with ease + Unicode support unlike everything else you can find. Since there was nothing like this I release it here for you.

For your WinAPI module


Class code or in a helper module


If this is too complex for you, I recommend to learn the basics and to read the parameters. Have fun with this! 🤝
Its an old method, goes back as far as 10+ years, i suppose its still valid. i can help you extend this library to a more advanced use if you want, you know where to find me (signature)
I personally use it in most of my projects. The performance is quite good even for a few hundred settings to read or write.
It is still valid and supported on all systems. So a good reason to use this, especially because it is very simple.

Thanks for your offer, but if I would need otherwise I would already have done this.
(Apr 5, 2017 03:11 AM)Visual Freedom Wrote:  I personally use it in most of my projects. The performance is quite good even for a few hundred settings to read or write.
It is still valid and supported on all systems. So a good reason to use this, especially because it is very simple.

Thanks for your offer, but if I would need otherwise I would already have done this.

Well alright, if you think u wanna up ur game check out

https://github.com/TheBarret/Xconf

gl ^^
One question to your bigger project. Do you handle that language specific systems write numbers with "," or "."?
Anyone with other feedback? Especially beginners should benefit from this easy to use code.
Are there any other people who would like to leave some feedback?
Personaly I used this : https://pastebin.com/91jwdLbL
Actualy I prefer to use Xml serialization.

;)
Why do you keep bumping your thread? Are you so proud of your release that you want people to see it?
(Apr 8, 2017 07:53 AM)Visual Freedom Wrote:  Especially beginners should profit from this easy to use code.

Profit? Do you mean benefit?
Need a custom software? Check out my freelance service here! 6+ years and counting, never a complaint :)

STOP STEALING CODE.

FREE NATHAN

People who report posts are lamers.
Yes I mean benefit. I edited my previous post.
What you did is fill in the parameters to call a prewritten API. Why'd you bump your thread 3 times for that? Am I missing something?
All credits to OP, because I had previously not heard of this API method. Here's how I would do it:

Code
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.IO;
using System.Text;

namespace HackForums {

    public class ConfigFile {
        
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern IntPtr WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
        
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern IntPtr GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName);
        
        public string Filename { get; private set; }
        public string Name { get; private set; }
        
        public ConfigFile(string name, string filename) {
            this.Name = name;
            this.Filename = filename;
        }
        
        public ConfigFile(Type t) {
            this.Name = t.Name;
            this.Filename = Path.GetFullPath(string.Format("{0}.ini", t.Name));
        }
        
        public string GetValue(string key, string @default) {
            StringBuilder sb = new StringBuilder(UInt16.MaxValue);
            IntPtr result = GetPrivateProfileString(this.Name, key, @default, sb, (uint)sb.Capacity, this.Filename);
            if(result != IntPtr.Zero) {
                return sb.ToString();
            }else {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
        
        public bool SetValue(string key, string @value) {
            if(@value.Length > UInt16.MaxValue) {
                throw new Exception("Key value too long!");
            }
            IntPtr result = WritePrivateProfileString(this.Name, key, @value, this.Filename);
            if(result != IntPtr.Zero) {
                return true;
            }else {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
    }
    
    public class Program {

        public static void Main(string[] argv) {
            
            /*
                Easy constructor
            */
            var config = new ConfigFile(typeof(Program));
            
            /*
                How to set a value
            */
            config.SetValue("Max turbo setting", "20"); //key name, value
            
            /*
                How to read a value
            */
            var turbo = config.GetValue("Max turbo setting", "540"); //key name, default value
            
            /*
                Print it out
            */
            Console.WriteLine("Turbo value = " + turbo);
        }
        
    }
    
}
[Image: Syr3DlV.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by Drizzy‎‎May 29, 2026 02:17 AM 61 4,758 May 29, 2026 02:17 AM
Last Post: dvs1626
by TheArchitectDec 31, 2022 02:38 AM 12 302 Dec 31, 2022 02:38 AM
Last Post: TheArchitect
[C#] by SIM-4785719Jul 27, 2020 06:40 PM 6 213 Jul 27, 2020 06:40 PM
Last Post: gigajew