当前位置: 代码迷 >> 综合 >> c# The type or namespace name ‘DllImport’ could not be found
  详细解决方案

c# The type or namespace name ‘DllImport’ could not be found

热度:22   发布时间:2024-01-11 11:36:48.0

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”)] 需要放在类里面使用

  相关解决方案