-1

At school we are currently learning GitHub and are therefore programming C#, which I learned last year. In the meantime, however, I have forgotten some things. Task, I have to create 2 methods:

A static bool LoadConfig method (ref int number): Here the standard configuration file config.ini is read out and the number is returned. Should the file do not exist or do not contain a number, the number 0 is returned and a return value false. Otherwise the return value is true.

One method static bool SaveConfig (int number): Here the standard configuration file config.ini is created or changed and the number entered. Should the If the file cannot be created, a return value is returned false. Otherwise the return value is true.

Code:

    static bool SaveConfig(int number)
            {
            var path = @"C:\Users\KarDan02\Desktop\Collide_d\Coll_1_ide";
            string text = Convert.ToString(number); 

            File.WriteAllText(path, text);

            if (File.Exists(path))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool LoadConfig(ref int number)
        {
            var path = @"C:\Users\KarDan02\Desktop\config.ini";
            string numbertext = File.ReadAllText(path);
            int controll = Convert.ToInt32(numbertext); 

            if ((File.Exists(pfad)) & (controll> 0))
            {
                number = controll; 
                return true;            
            }
            else
            {
                number = 0;
                return false;  
            }
        }

Problems: A) The path can only be used by you. Could you do it so that the .ini file is saved and read into the current directory of the program? How do you create a dynamic location? Because it actually depends on the user in which folder he initializes the Github file. Or can you possibly save it in the C # storage location or the location where the project is?

B) You change the value of number in any case (even if the ini file has a 0 in it).

Sebastian Inones
  • 1,561
  • 1
  • 19
  • 32
02KarDan
  • 1
  • 3

1 Answers1

0

Welcome 02KarDan!

Instead of ini file I would suggest you use standard application configuration settings which takes care about configuration location, setting variable data type, scope and default value. If you want to save your user settings is important to select the scope User.

Under your project Properties, select Settings tab and create a new settings by clickink the linked text. VS will create app.config file. Then add settings to the grid (Visual Studio 2019).

enter image description here

Then you can easily read or save settings. Here is a simple example of console application:

class Program
{
    static void Main()
    {
        ShowNumberOfCars();
        UpdateNumberOfCars(10);
        ShowNumberOfCars();
    }

    private static void ShowNumberOfCars()
    {
        var number = Properties.Settings.Default.NumberOfCars;
        Console.WriteLine($"Number of cars: {number}"); 
    }

    private static void UpdateNumberOfCars(int value)
    {
        Properties.Settings.Default.NumberOfCars = value;
        Properties.Settings.Default.Save();
    }
}

More information is documented for example on this page: https://learn.microsoft.com/en-us/visualstudio/ide/how-to-add-app-config-file?view=vs-2019.

Tomas Paul
  • 490
  • 5
  • 13
  • But the probem is that the method should create and read a .ini file – 02KarDan May 04 '20 at 21:24
  • You are right, that is not solving your problem, just offers alternative way. Maybe look here https://stackoverflow.com/questions/217902/reading-writing-an-ini-file for ini handling. – Tomas Paul May 05 '20 at 08:05