错误:必须声明一个正文,因为它没有标记为abstract,extern或partial

时间:2013-10-31 17:56:55

标签: c#

代码会产生以下错误:

  

错误:Ini.IniFile.WritePrivateProfileString(string,string,string,string)'必须声明一个正文,因为它没有标记为abstract,extern或partial

     

错误2:Ini.IniFile.GetPrivateProfileString(string,string,string,System.Text.StringBuilder,int,string)'必须声明一个正文,因为它没有标记为abstract,extern或partial

using System.Runtime.InteropServices;
using System.Text;

namespace Ini
{
  public class IniFile
  {
    public string path;

    public IniFile(string INIPath)
    {
      this.path = INIPath;
    }

    [DllImport("kernel32")]
    private static long WritePrivateProfileString(string section, string key, string val, string filePath);

    [DllImport("kernel32")]
    private static int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

    public void IniWriteValue(string Section, string Key, string Value)
    {
      IniFile.WritePrivateProfileString(Section, Key, Value, this.path);
    }

    public string IniReadValue(string Section, string Key)
    {
      StringBuilder retVal = new StringBuilder((int) byte.MaxValue);
      IniFile.GetPrivateProfileString(Section, Key, "", retVal, (int) byte.MaxValue, this.path);
      return ((object) retVal).ToString();
    }
  }
}

1 个答案:

答案 0 :(得分:1)

使用PInvoke调用本机方法时,需要包含extern。例如:

static extern bool WritePrivateProfileString(string lpAppName,
   string lpKeyName, string lpString, string lpFileName);

http://www.pinvoke.net/default.aspx/kernel32.writeprivateprofilestring