C# 试图读取或写入受保护的内存。这通常表示在创建INI文件时其他内存已损坏
最后,我不得不在我的项目中使用INI文件来存储一些数据。所以我在我的项目中用不同的名称空间创建了一个类。现在,当我试图执行我的项目时,我得到了这个错误。我正在使用Microsoft Visual C#2010 Express。我的代码是:C# 试图读取或写入受保护的内存。这通常表示在创建INI文件时其他内存已损坏,c#,ini,C#,Ini,最后,我不得不在我的项目中使用INI文件来存储一些数据。所以我在我的项目中用不同的名称空间创建了一个类。现在,当我试图执行我的项目时,我得到了这个错误。我正在使用Microsoft Visual C#2010 Express。我的代码是: namespace Ini { public class IniFile { public string path; [DllImport("kernel32")] private static
namespace Ini
{
public class IniFile
{
public string path;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key,int val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key,string def, StringBuilder retVal,
int size,string filePath);
public IniFile(string IniPath)
{
path = IniPath;
}
public void IniWriteValue(string Section, string Key, int Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
return temp.ToString();
}
}
}
我正在我的主要项目中使用这个。。
使用Ini;(在命名空间中)
和
IniFile MyIni = new IniFile("D:\\Database.ini");
MyIni.IniWriteValue("ProductBase", "Key", 1);
(在我的代码中)正如您在和中所看到的,WritePrivateProfileString()
有四个字符串参数,因此将您的定义更改为
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key, string val, string filePath);
以及
public void IniWriteValue(string Section, string Key, int Value)
{
WritePrivateProfileString(Section, Key, Value.ToString(), this.path);
}
文档中有这样一句话:“此函数仅为与16位版本的Windows兼容而提供。应用程序应在注册表中存储初始化信息。”可能使用不需要调用kernel32.dll
的API?在普通应用程序中,我觉得很奇怪。