先声明winapi:
[DllImport("kernel32.dll",EntryPoint="GetPrivateProfileString")]
static extern int GetPrivateProfileString(string strApplicationName,string   strKeyName,string strDefalult, StringBuilder strReturnedString,long nSize,string strFileName);然后调用api:StringBuilder strValue=new StringBuilder(100);
int result=GetPrivateProfileString("DatabaseConnetion","ServerName","",  strValue,100,@"d:\parameter.ini");
怎么调用api 函数后,strValue值还为空呀。我的配置文件(parameter.ini)的内容如下:
[Option]
Encryption=True     
[DatabaseConnetion]
ServerName=etpmtiblp
DatabaseType=PvnYfssjt
DatabaseName=pig
UserName=pf
password=pf
[Others]
Staff_ID=2
Staff_Name=^iooojpytgupo

解决方案 »

  1.   

    用这个改一下试试using System;
    using System.Runtime.InteropServices;
    using System.Text;
    namespace GPPS
    {
       class Class1
       {
         static void Main(string[] args)
         {
           StringBuilder sb = new StringBuilder(500);
           uint res = GetPrivateProfileString("AppName", "KeyName", "", sb, sb.Capacity, @"c:\test.ini");
           Console.WriteLine(sb.ToString());
         }
           [DllImport("kernel32.dll")]
           static extern uint GetPrivateProfileString(
         string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
       }
    }
      

  2.   

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

    string ProFile =Application.StartupPath+"\\profile.ini";
    string connstr="";
    string DataSource="" ;
                string Password="" ;
                string UserID="";
                StringBuilder temp = new StringBuilder ( 255 );
                int L ;
    L = GetPrivateProfileString("SCSYY", "DATASOURCE", "-1",temp, 255, ProFile);
                DataSource = temp.ToString();
                DataSource=DataSource.Substring(0,L);
    if (DataSource.Trim()=="")
    {
      MessageBox.Show("Porfile Error DataSource");
    return "-1";
    }
    StringBuilder temp1 = new StringBuilder ( 255 );
    L=0;
    L = GetPrivateProfileString("SCSYY", "USERID", "-1",temp1, 255, ProFile);
    UserID = temp1.ToString();
    UserID= UserID.Substring(0,L);
    if (UserID.Trim()=="")
    {
    MessageBox.Show("Porfile Error UserID");
    return "-1";
    }
    StringBuilder temp2 = new StringBuilder ( 255 );
        L=0;
    L = GetPrivateProfileString("SCSYY", "PASSWORD", "-1",temp2, 255, ProFile);
    Password = temp2.ToString();
    Password=Password.Substring(0,L);
    if (Password.Trim()=="")
    {
      MessageBox.Show("Porfile Error Password");
      return "-1";
    }
                 connstr="user id="+UserID+";data source="+DataSource+";password="+Password;
    return  connstr;