为什么我的代码没有写入 .ini 文件?

Why is my code not writing into the .ini file?

我目前正在用 C# 编写我的第一批代码。我希望我的代码将一些值(用于设置)保存到配置文件漫游文件夹中的 .ini 文件中。没有错误。但是当我 运行 我的代码时,.ini 文件没有任何变化。

我的代码:

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");
}

StacksOverflow 中使用 .ini 的代码:

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;
        }
    }
}

解决方案:

我发现,我不需要创建 ini 文件。代码如下:

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");
}
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");
}

就那么一行...