c# The type or namespace name ‘DllImport’ could not be found
C#调用系统库时,提示“The type or namespace name ‘DllImport’ could not be found”,代码如下:
namespace DeployAssist.Util
{public class IniFileHelper{private string strIniFilePath; // ini配置文件路径//返回0表示失败,非0为成功[DllImport("kernel32", CharSet = CharSet.Auto)]private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);//返回取得字符串缓冲区的长度[DllImport("kernel32", CharSet = CharSet.Auto)]private static extern long GetPrivateProfileString(string section, string key, string strDefault, StringBuilder retVal, int size, string filePath);[DllImport("kernel32.dll", CharSet = CharSet.Auto)]private static extern int GetPrivateProfileInt(string section, string key, int nDefault, string filePath);
解决方法:
1:一定要 using System.Runtime.InteropServices;
2: [DllImport(“XXXX”)] 需要放在类里面使用