Hack Forums
[.NET] Save INI files with ease. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Save INI files with ease. (/showthread.php?tid=5592610)



Save INI files with ease. - Visual Freedom - Apr 5, 2017

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! 🤝


* - Barret - Apr 5, 2017

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)


* - Visual Freedom - Apr 5, 2017

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.


* - Barret - Apr 5, 2017

(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 ^^


* - Visual Freedom - Apr 5, 2017

One question to your bigger project. Do you handle that language specific systems write numbers with "," or "."?


* - Visual Freedom - Apr 8, 2017

Anyone with other feedback? Especially beginners should benefit from this easy to use code.


* - Visual Freedom - Apr 16, 2017

Are there any other people who would like to leave some feedback?


* - sh@rp - Apr 16, 2017

Personaly I used this : https://pastebin.com/91jwdLbL
Actualy I prefer to use Xml serialization.

;)


* - Ninja - Apr 16, 2017

Why do you keep bumping your thread? Are you so proud of your release that you want people to see it?


* - idb. - Apr 16, 2017

(Apr 8, 2017 07:53 AM)Visual Freedom Wrote:  Especially beginners should profit from this easy to use code.

Profit? Do you mean benefit?


* - Visual Freedom - Apr 16, 2017

Yes I mean benefit. I edited my previous post.


* - log dot zip - Apr 16, 2017

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?


* - gigajew - Apr 17, 2017

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