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

[Solved] C# The type or namespace name ‘DllImport’ could not be found

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

The type or namespace name ‘DllImport’ could not be found” when calling the system library in C#, the code is as follows.

namespace DeployAssist.Util
{
    public class IniFileHelper
    {
        private string strIniFilePath;  // ini configuration file path

        //returns 0 for failure, non-zero for success
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //Returns the length of the obtained string buffer
        [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);

Solution:

1: Be sure to use system. Runtime. Interopservices

2: [dllimport (“XXX”)] needs to be used in a class