How to read and write data to an INI file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mylixes
    New Member
    • May 2010
    • 29

    How to read and write data to an INI file

    I am currently developing a windows application. I have a form that saves data to an ini file.

    I am currently having a trouble in writing and reading ini file.

    First, if the form loaded, it will read the ini file with corresponding section. it works well. But when I save / write data (one of it's key is a path (filename of the image))to ini file, changes does not reflect. When I closes the form and load it again, the data will be read as what i have saved though the changes doesn't reflect in ini file.

    But, if I'm going to save the data and the path (file name of the image) is not included, changes reflects to the ini file.

    I don't understand why is it happening.

    I really need help here.

    Below are my codes.

    Save data to ini:
    Code:
    private void Save()
            {
                try
                {               
                    IniFile.WritePrivateProfileString("MERCHANTINFO", "IMAGEPATH", imageFileName, ".\\MPC.ini");
                    IniFile.WritePrivateProfileString("MERCHANTINFO", "MERCHANTNAME", txtName.Text, ".\\MPC.ini");
                    IniFile.WritePrivateProfileString("MERCHANTINFO", "MERCHANTCODE", txtMerchantCode.Text, ".\\MPC.ini");
                    IniFile.WritePrivateProfileString("MERCHANTINFO", "SHOPID", txtShopID.Text, ".\\MPC.ini");
                    IniFile.WritePrivateProfileString("MERCHANTINFO", "TERMINALID", txtTerminalID.Text, ".\\MPC.ini");
                    IniFile.WritePrivateProfileString("MERCHANTINFO", "TEL", txtTel.Text, ".\\MPC.ini");
                    IniFile.WritePrivateProfileString("MERCHANTINFO", "FAX", txtFax.Text, ".\\MPC.ini");
                    IniFile.WritePrivateProfileString("MERCHANTINFO", "ADDRESS", txtAddress.Text, ".\\MPC.ini");
                    IniFile.WritePrivateProfileString("MERCHANTINFO", "WEBSITE", txtWebsite.Text, ".\\MPC.ini");
                    
                    Common.FuncShowInfoMsg("Merchant informations succesfully saved.", "Save Successful");
    
                    btnSave.Enabled = false;
                    btnCancel.Text = "Close";
                    btnCancel.Focus();
    
                }
                catch (Exception ex)
                {
                    Common.FuncShowErrorMsg("Unable to save Merchant Info.", ex.Message, "Failed");
                }
            }
    Reading ini file:
    Code:
    Common.sbName = new StringBuilder(1024);
               Common.sbShopID = new StringBuilder(1024);
               Common.sbTel = new StringBuilder(1024);
               Common.sbTermID = new StringBuilder(1024);
               Common.sbWebsite = new StringBuilder(1024);
               Common.sbImageFileName = new StringBuilder(1024);
               Common.sbFax = new StringBuilder(1024);
               Common.sbAddress = new StringBuilder(1024);
               Common.sbCode = new StringBuilder(1024);
    
               IniFile.GetPrivateProfileString("MERCHANTINFO", "MERCHANTNAME", "", Common.sbName, 1024, ".\\MPC.ini");
               
    
               IniFile.GetPrivateProfileString("MERCHANTINFO", "MERCHANTCODE", "", Common.sbCode, 1024, ".\\MPC.ini");
               
    
               IniFile.GetPrivateProfileString("MERCHANTINFO", "SHOPID", "", Common.sbShopID, 1024, ".\\MPC.ini");
              
    
               IniFile.GetPrivateProfileString("MERCHANTINFO", "TERMINALID", "", Common.sbTermID, 1024, ".\\MPC.ini");
               
    
               IniFile.GetPrivateProfileString("MERCHANTINFO", "TEL", "", Common.sbTel, 1024, ".\\MPC.ini");
               
    
               IniFile.GetPrivateProfileString("MERCHANTINFO", "FAX", "", Common.sbFax, 1024, ".\\MPC.ini");
               
    
               IniFile.GetPrivateProfileString("MERCHANTINFO", "ADDRESS", "", Common.sbAddress, 1024, ".\\MPC.ini");
               
    
               IniFile.GetPrivateProfileString("MERCHANTINFO", "WEBSITE", "", Common.sbWebsite, 1024, ".\\MPC.ini");
               
    
               IniFile.GetPrivateProfileString("MERCHANTINFO", "IMAGEPATH", "", Common.sbImageFileName, 1024, ".\\MPC.ini");
Working...