0

I've learned a lot over the past few months mainly on this site. I'm getting into a little hole here with this one though. I'm trying to write or change the values in a specific INI file.

I'm using a custom class made by a StackOverflow Guru by the name of "Danny Beckett" that I really like and understand. Been using this for a while now with no issue. Works perfectly for most anything while writing and reading INI files... Until now.

This is where he talks about it. Reading/writing an INI file

This is the Class I'm using to read and write the INI's.

    using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

// Change this to match your program's normal namespace
namespace MyProg
{
    class IniFile   // revision 10
    {
        string Path;
        string EXE = Assembly.GetExecutingAssembly().GetName().Name;

        [DllImport("kernel32")]
        static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);

        [DllImport("kernel32")]
        static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);

        public IniFile(string IniPath = null)
        {
            Path = new FileInfo(IniPath ?? EXE + ".ini").FullName.ToString();
        }

        public string Read(string Key, string Section = null)
        {
            var RetVal = new StringBuilder(255);
            GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
            return RetVal.ToString();
        }

        public void Write(string Key, string Value, string Section = null)
        {
            WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
        }

        public void DeleteKey(string Key, string Section = null)
        {
            Write(Key, null, Section ?? EXE);
        }

        public void DeleteSection(string Section = null)
        {
            Write(null, null, Section ?? EXE);
        }

        public bool KeyExists(string Key, string Section = null)
        {
            return Read(Key, Section).Length > 0;
        }
    }
}

This is the code I'm using to actually make the changes....

var MyIni = new IniFile("Settings");
var SelectedPdata = MyIni.Read("SelectedPdata");
var SelectedSQL = MyIni.Read("SelectedSQL");
var SetupIniLocation = SelectedPdata + "\\Setup\\Sidexis\\Setup.ini";
var SetupIni = new IniFile(SetupIniLocation)  
SetupIni.DeleteKey("VZPDATA", "CLIENT");
SetupIni.Write("VZPDATA", SelectedPdata, "CLIENT");
SetupIni.DeleteKey("COMBOSQL", "CLIENT");
SetupIni.Write("COMBOSQL", SelectedSQL, "CLIENT");

What is does: It reads two Parameters from my settings INI file and passes them to variables. Then uses those variables to write them into the setup.ini file.

The target "Setup.ini"

[CLIENT]
Aufruf="msiexec.exe /i Sidexis.MSI VZSIDEXIS="C:\Sidexis\" SQLDIALOGTYPE=1 LANGUAGE=1033 PROPDSN="PDATA_SQLEXPRESS" PROPDBO="" INSTALLATIONTYPE=FULL SERIALNUMBER=6000000837 VZPDATA="\\JONMER-WIN7X64\PDATA\" COMBOSQL="JONMER-WIN7X64\PDATA_SQLEXPRESS" COMPANYNAME="" NEEDEDDISKSPACE=104857600 MASTERCLIENT=TRUE /QB!"

The paramaters that I need to modify are: COMBOSQL and VZPDATA.

It is able to modify it... But it just puts them at the bottom. I'm sure it has something to do with the formatting because if I format it like a regular INI file. (each parameter on its own line) It works fine....

Community
  • 1
  • 1
Redracer68
  • 958
  • 1
  • 7
  • 12

0 Answers0