Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

issues with script for saving file to folder.

Discussion in 'Scripting' started by welpie21, Mar 27, 2016.

  1. welpie21

    welpie21

    Joined:
    Dec 23, 2014
    Posts:
    49
    hello guys... i've been working like 2 days to edit the script. and gettings to work..

    the scripts its just making a new folder and file.. when the folder doesn't excist it makes a new folder that part is working.. but the file is the problem.. sometimes if run the game.. its checks also when the file excists or not...
    i wanted actually that when the file is created it needs to be created inside the folder path.. and the folder path is in the documents.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System.Collections.Generic;
    5. using System;
    6.  
    7. public class INIWorker : MonoBehaviour {
    8.     public static string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "settings.ini");
    9.     //private static string path = Path.Combine(Application.persistentDataPath, "C:/Users/" + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "settings.ini");
    10.     private static Dictionary<string, Dictionary<string, string>> IniDictionary = new Dictionary<string, Dictionary<string, string>>();
    11.     private static bool Initialized = false;
    12.     /// <summary>
    13.     /// Sections list
    14.     /// you can acces the [name] in the inifile whats created.
    15.     /// </summary>
    16.     public enum Sections {
    17.         settings,
    18.     }
    19.     /// <summary>
    20.     /// Keys list
    21.     /// you can acces the variables whats under the [name].
    22.     /// </summary>
    23.     public enum Keys {
    24.         volume,
    25.         toggleVolume,
    26.         AntiAliasing,
    27.     }
    28.  
    29.     private static bool FirstRead() {
    30.         if (File.Exists(path)) {
    31.             using (StreamReader sr = new StreamReader(path)) {
    32.                 string line;
    33.                 string theSection = "";
    34.                 string theKey = "";
    35.                 string theValue = "";
    36.                 while (!string.IsNullOrEmpty(line = sr.ReadLine())) {
    37.                     line.Trim();
    38.                     if (line.StartsWith("[") && line.EndsWith("]"))
    39.                     {
    40.                         theSection = line.Substring(1, line.Length - 2);
    41.                     }
    42.                     else
    43.                     {
    44.                         string[] ln = line.Split(new char[] { '=' });
    45.                         theKey = ln[0].Trim();
    46.                         theValue = ln[1].Trim();
    47.                     }
    48.                     if (theSection == "" || theKey == "" || theValue == "")
    49.                         continue;
    50.                     PopulateIni(theSection, theKey, theValue);
    51.                 }
    52.             }
    53.         }
    54.         return true;
    55.     }
    56.  
    57.     private static void PopulateIni(string _Section, string _Key, string _Value) {
    58.         if (IniDictionary.ContainsKey(_Section)) {
    59.             if (IniDictionary[_Section].ContainsKey(_Key))
    60.                 IniDictionary[_Section][_Key] = _Value;
    61.             else
    62.                 IniDictionary[_Section].Add(_Key, _Value);
    63.         } else {
    64.             Dictionary<string, string> neuVal = new Dictionary<string, string>();
    65.             neuVal.Add(_Key.ToString(), _Value);
    66.             IniDictionary.Add(_Section.ToString(), neuVal);
    67.         }
    68.     }
    69.     /// <summary>
    70.     /// Write data to INI file. Section and Key no in enum.
    71.     /// </summary>
    72.     /// <param name="_Section"></param>
    73.     /// <param name="_Key"></param>
    74.     /// <param name="_Value"></param>
    75.     public static void IniWriteValue(string _Section, string _Key, string _Value) {
    76.         if (!Initialized)
    77.             FirstRead();
    78.         PopulateIni(_Section, _Key, _Value);
    79.         //write ini
    80.         WriteIni();
    81.     }
    82.     /// <summary>
    83.     /// Write data to INI file. Section and Key bound by enum
    84.     /// </summary>
    85.     /// <param name="_Section"></param>
    86.     /// <param name="_Key"></param>
    87.     /// <param name="_Value"></param>
    88.     public static void IniWriteValue(Sections _Section, Keys _Key, string _Value) {
    89.         IniWriteValue(_Section.ToString(), _Key.ToString(), _Value);
    90.     }
    91.  
    92.     private static void WriteIni() {
    93.         using (StreamWriter sw = new StreamWriter(path)) {
    94.             foreach (KeyValuePair<string, Dictionary<string, string>> sezioni in IniDictionary) {
    95.                 sw.WriteLine("[" + sezioni.Key.ToString() + "]");
    96.                 foreach (KeyValuePair<string, string> chiave in sezioni.Value) {
    97.                     // value must be in one line
    98.                     string vale = chiave.Value.ToString();
    99.                     vale = vale.Replace(Environment.NewLine, " ");
    100.                     vale = vale.Replace("\r\n", " ");
    101.                     sw.WriteLine(chiave.Key.ToString() + " = " + vale);
    102.                 }
    103.             }
    104.         }
    105.     }
    106.     /// <summary>
    107.     /// Read data from INI file. Section and Key bound by enum
    108.     /// </summary>
    109.     /// <param name="_Section"></param>
    110.     /// <param name="_Key"></param>
    111.     /// <returns></returns>
    112.     public static string IniReadValue(Sections _Section, Keys _Key) {
    113.         if (!Initialized)
    114.             FirstRead();
    115.         return IniReadValue(_Section.ToString(), _Key.ToString());
    116.     }
    117.     /// <summary>
    118.     /// Read data from INI file. Section and Key no in enum.
    119.     /// </summary>
    120.     /// <param name="_Section"></param>
    121.     /// <param name="_Key"></param>
    122.     /// <returns></returns>
    123.     public static string IniReadValue(string _Section, string _Key) {
    124.         if (!Initialized)
    125.             FirstRead();
    126.         if (IniDictionary.ContainsKey(_Section))
    127.         if (IniDictionary[_Section].ContainsKey(_Key))
    128.             return IniDictionary[_Section][_Key];
    129.         return null;
    130.     }
    131. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4. using System.IO;
    5. using System.Text;
    6.  
    7. public class INIMaker : MonoBehaviour {
    8.  
    9.     public volume vol;
    10.     public Anti_Aliasing aa;
    11.     public toggle_audio ta;
    12.  
    13.     [Header("path for creating")]
    14.     public string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "settings.ini");
    15.     public string folderName;
    16.  
    17.     [Header("the name of file and folder")]
    18.     public string nameOfFile;
    19.     public string nameOfFolder;
    20.  
    21.     [Header("string storage")]
    22.     public string[] write;
    23.  
    24.  
    25.     void Awake()
    26.     {
    27.         if (Directory.Exists(nameOfFolder))
    28.         {
    29.             string pathStringMap = Path.Combine (folderName, folderName);
    30.             Directory.CreateDirectory (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "HQDefence"));
    31.  
    32.             if (!File.Exists (nameOfFile)) {
    33.                 string pathStringFile = Path.Combine (fileName, "settings.ini");
    34.                 File.Create (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"HQDefence/settings.ini"));//pathStringFile);
    35.                 WriteToIni ();
    36.             }
    37.         }
    38.     }
    39.  
    40.  
    41.     void WriteToIni(){
    42.         INIWorker.IniWriteValue ("settings", "volume", "0");
    43.         INIWorker.IniWriteValue ("settings", "toggleVolume", "false");
    44.         INIWorker.IniWriteValue ("settings", "AntiAliasing", "0");
    45.     }
    46.  
    47. }
    48.  
    the first code i downloaded it somewhere..
    and the second code i coded by my self.

    so i hope you guys can fix this issues and i hope my english is not that worse :p

    have a nice day,
    beau
     
  2. welpie21

    welpie21

    Joined:
    Dec 23, 2014
    Posts:
    49
    sorry.... i will explain the two scripts :

    script 1 (INIWorker.cs) :
    this script checks the sections and the strings and also the value whats put in the ini file..

    script 2 (INIMakers.cs) :
    this script makes the folder and the ini file.. if you see that. in the function of WriteToIni is actually when the file is created the function will call that its need to write inside the ini file..

    i hope this is enough information