- Home /
Trouble with writing text to a .ini file
I am working on a small game project and I am trying to write a class that will allow a settings menu to write the recorded settings to a .ini file. However, I can't seem to get it to write to the file. I think I wrote the class correctly to do it based upon the MSDN article on the subject. But it just doesn't seem to work. Here is my class:
 using System;
 using System.IO;
 using System.Runtime.InteropServices;
 using System.Text;
 using UnityEngine;
 
 //Used to allow other files to access the script by name
 namespace INI
 
 {
 public class iniFile {
     public string path;
 
     string winDir=System.Environment.GetEnvironmentVariable("windir");
 
     [DllImport("KERNEL32.DLL")]
     private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
     [DllImport("KERNEL32.DLL")]
     private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 
     /// INIFile Constructor.
     public iniFile(string INIPath) {
         path = INIPath;
     }
     /// Write Data to the INI File
     public void IniWriteValue(string Section, string Key, string Value) {
         long success = WritePrivateProfileString(Section, Key, Value, this.path);
         Debug.Log (Section);
         Debug.Log (Key);
         Debug.Log (Value);
         Debug.Log (path);
         Debug.Log (success);
         return;
     }
 
     /// Read Data Value From the Ini File
     public string IniReadValue(string Section, string Key) {
         StringBuilder temp = new StringBuilder(255);
         int i = GetPrivateProfileString(Section,  Key, "", temp, 255, this.path);
         return temp.ToString();
     }
 }
 }
Here is my call for the class in the main menu script:
 if (GUI.Button (new Rect (Screen.width * guiGameplayApplyButtonPlaceX, Screen.height *    guiGameplayApplyButtonPlaceY, Screen.width * guiGameplayApplyButtonSizeX, Screen.height *     guiGameplayApplyButtonSizeY), "Apply")) {
         ini.IniWriteValue("Gameplay","Difficulty","Test");
         print ("Apply");
     }
I created the instance of the class like this:
 iniFile ini = new iniFile(@"..\settings\settingsTest.ini");
GetEnvironmentVariable("windir") and [DllImport("$$anonymous$$ERNEL32.DLL")] look extremely unportable. You also don't check for success and whatever i is supposed to be.
On that note, are you sure the folder that your supposed file is supposed to exist in also exists?
Sorry for the lag in response, I wasn't even sure if a moderator had finally approved this to be posted. To Graham Dunnett's question: I do not want to use playerPrefs because it uses the registry and using the registry is not the way I want the game to store settings. It makes it difficult to change settings quickly (without having to modify them in the code). I had planned on making a dev console to change settings that I don't want the player to access. ini files are what I am more familiar with.
To benproductions1 answer: I am not planning on porting the game to any other platform, so using that .dll file is not a problem for me. The folder does exist. However, that isn't a problem anyway because it will create the file if it doesn't exist already.
Also to your question about checking for success. I do cheeck and just send it to the debug log. It checks here:
 long success = WritePrivateProfileString(Section, $$anonymous$$ey, Value, this.path);
         Debug.Log (Section);
         Debug.Log ($$anonymous$$ey);
         Debug.Log (Value);
         Debug.Log (path);
         Debug.Log (success);
 
 
This shows up in the class.
As far as the "i" variable goes, I am not exactly sure what it does. It was show in the $$anonymous$$SDN article as needing to be there. However, it is irrelevant. It is in the function for reading the file not writing.
Answer by Namey5 · Nov 24, 2014 at 10:23 AM
You could just use using System.IO, and write:
 File.WriteAllLines(*path you want*, string [] {"Gameplay", "Difficulty", "Test"});
Note I use JavaScript primarily, and in JavaScript, the line is just:
 System.IO.File.WriteAllLines(*path you want*, ["Gameplay", "Difficulty", "Test"])
I'm not too sure on using arrays in C# but I beleive that is how you do it.
Your answer
 
 
             Follow this Question
Related Questions
How to make main menu including settings? 0 Answers
The application icon is always 32x32 - why? 1 Answer
How make a Settings Menu? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                