C# Error CS0601 – The DllImport attribute must be specified on a method marked ‘static’ and ‘extern’

C# Error

CS0601 – The DllImport attribute must be specified on a method marked ‘static’ and ‘extern’

Reason for the Error & Solution

The DllImport attribute must be specified on a method marked ‘static’ and ‘extern’

The DllImport attribute was used on a method that did not have the correct access keywords.

The following sample generates CS0601:

// CS0601.cs  
using System.Runtime.InteropServices;  
using System.Text;  
  
public class C  
{  
   [DllImport("KERNEL32.DLL")]  
   extern int GetCurDirectory(int bufSize, StringBuilder buf);   // CS0601  
   // Try the following line instead:  
   // static extern int GetCurDirectory(int bufSize, StringBuilder buf);  
}  
  
public class MainClass  
{  
   public static void Main ()  
   {  
   }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...