Reputation: 25
I'm currently writing some of my first codes in c#. I want my code to save some values (used for settings) into an .ini file in the profile roaming folder. There are not errors. But when I run my code, there are no changes in the .ini file.
My Code:
private void LoadSettings()
{
var userprofile_location = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Appdata\Roaming\GameCentral";
Directory.CreateDirectory(userprofile_location);
File.Create(userprofile_location + @"\settings.ini");
IniFile settings = new IniFile(userprofile_location + @"\settings.ini");
settings.Write("1","PFAD","Icons");
}
The Code from StacksOverflow to use the .ini's:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace GameCentral
{
class IniFile
{
string Path;
string EXE = Assembly.GetExecutingAssembly().GetName().Name;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
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;
}
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;
}
}
}
Solution:
I found out, that i don't need to create the ini file. Code would be like:
private void LoadSettings()
{
var userprofile_location = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Appdata\Roaming\GameCentral";
Directory.CreateDirectory(userprofile_location);
//File.Create(userprofile_location + @"\settings.ini");
var settings = new IniFile(userprofile_location + @"\settings.ini");
settings.Write("1","path","Icons");
}
Upvotes: 0
Views: 838
Reputation: 25
private void LoadSettings()
{
var userprofile_location = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\Appdata\Roaming\GameCentral";
Directory.CreateDirectory(userprofile_location);
//File.Create(userprofile_location + @"\settings.ini");
var settings = new IniFile(userprofile_location + @"\settings.ini");
settings.Write("1","path","Icons");
}
It was just that one line...
Upvotes: 1