使用ini文件c#时如何在等号之间包含空格?
How to include spaces between equal sign when working with ini files c#?
您好,我有一个格式如下的 ini 文件
[Text]
abcd = 1234
text = 1002
some = 4414
last = 1824
然而,当我使用 ini 文件 class 时,我在网上找到一个 class 用于处理 ini 文件:
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
// Change this to match your program's normal namespace
namespace Program
{
class IniFile // revision 10
{
string Path;
string EXE = Assembly.GetExecutingAssembly().GetName().Name;
[DllImport("kernel32")]
static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);
[DllImport("kernel32")]
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.ToString();
}
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 文件中,但是它的格式如下:
test=0010
除了写入函数创建的那些之外,读取函数也不起作用。
我如何才能更改代码,使其在等号前后放置 spaces?在值之前添加一个 space 但在键之后添加一个无效。此外,我对在值中添加 spaces 犹豫不决,因为我担心它可能会改变实际值并使我使用它的操作变得可读。
任何见解将不胜感激,谢谢。
这是另一个 IniFile class 可以让您实现该间距:https://github.com/MarioZ/MadMilkman.Ini
您需要做的是提供具有所需格式的 IniOptions,如下所示:
IniOptions options = new IniOptions();
options.KeySpaceAroundDelimiter = true;
IniFile ini = new IniFile(options);
ini.Load("path to your input INI file");
// Do something with file's sections and their keys ...
ini.Save("path to your output INI file");
您好,我有一个格式如下的 ini 文件
[Text]
abcd = 1234
text = 1002
some = 4414
last = 1824
然而,当我使用 ini 文件 class 时,我在网上找到一个 class 用于处理 ini 文件:
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
// Change this to match your program's normal namespace
namespace Program
{
class IniFile // revision 10
{
string Path;
string EXE = Assembly.GetExecutingAssembly().GetName().Name;
[DllImport("kernel32")]
static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);
[DllImport("kernel32")]
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.ToString();
}
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 文件中,但是它的格式如下:
test=0010
除了写入函数创建的那些之外,读取函数也不起作用。
我如何才能更改代码,使其在等号前后放置 spaces?在值之前添加一个 space 但在键之后添加一个无效。此外,我对在值中添加 spaces 犹豫不决,因为我担心它可能会改变实际值并使我使用它的操作变得可读。
任何见解将不胜感激,谢谢。
这是另一个 IniFile class 可以让您实现该间距:https://github.com/MarioZ/MadMilkman.Ini
您需要做的是提供具有所需格式的 IniOptions,如下所示:
IniOptions options = new IniOptions();
options.KeySpaceAroundDelimiter = true;
IniFile ini = new IniFile(options);
ini.Load("path to your input INI file");
// Do something with file's sections and their keys ...
ini.Save("path to your output INI file");