在你的class里面,随便找个地方,加上这些(举例):[DllImport("kernel32.dll")]
public static extern uint GetPrivateProfileStringA(
string lpAppName,
string lpKeyName,
string lpDefault,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
uint nSize,
string lpFileName);代码最前面,加上引用:using Microsoft.Win32;
然后在你的程序中随便引用就是了。下面是我的例子:
//没什么说的了,读取ini文件中指定section上的key的value
private static string ReadIniString(string fname,string secname,string keyname)
{
uint BufferSize=255;
byte[] tmp = new byte[0]; long bufsize = GetPrivateProfileStringA(secname,keyname,"",tmp,BufferSize,fname);
if(bufsize<=0)return "";
byte[] buf = new Byte[bufsize];
GetPrivateProfileStringA(secname,keyname,"",buf,BufferSize,fname);
return System.Text.Encoding.Default.GetString(buf);
}

解决方案 »

  1.   

    http://www.codeproject.com/csharp/c__and_api.asp
      

  2.   

    http://www.ccw.com.cn/htm/app/aprog/01_9_14_4.asp
      

  3.   

    using System;
    using System.Runtime.InteropServices; public class PinvokeApp
    { [DllImport("user32.dll",CharSet=CharSet.Unicode)]
    static extern int MessageBoxW(int hWnd,string msg,string caption,int type);
    //EntryPoint="MessageBoxA"
    public static void Main()
    {
    int x=MessageBoxW(0,"Hello,World!","This is called from a C# App",6);
    Console.Write(x); }
    }