I'm having quite the strange issue by trying to use Gdal in C#.
Here is my code, using the NuGet of GDAL in Visual Studio :
// Launch the Gdal Configuration + loading of drivers before everything
GdalConfiguration.ConfigureGdal();
GdalConfiguration.ConfigureOgr();
Gdal.AllRegister();
// Try to load the shapefile with "pathOfShapefile" being the string containing the path of the file
DataSource test = Ogr.Open(ConfigurationManager.AppSettings["pathOfShapefile"], GdalConst.GA_Update);
Sadly, I get an exception when running this piece of code. It tells me that it cannot load the DLL named "gdalconst_wrap".
Yet, the DLL is in its rightful place in the Debug folders. However, if I take it out of its specialized folder (bin/Debug/gdal/x64) to put it into the root of the Debug folder (bin/Debug), I get a new exception, telling me that there has been an "attempt to read or to write in protected memory".
I tried with the x86 version of the DLL, or with other versions found online on the official GDAL websites; sadly, that changes nothing.
So far, I've found a couple of threads on several forums of people seeming to have a similar problem; but nobody ever answered them, apparently. For myself, I'm quite stuck and would be very grateful for some help.
-- EDIT --
Apparently, the problem seems to be linked with the Gdal constant "GA_Update".
From what I gather, the "Open" function of Ogr require an instruction as to how the file should be opened : in read only, or in update mode. For this, it requires an object of type "int", which is part of the constants that Gdal uses as a second argument for the function. I used the constant "GA_Update" to indicate that I wanted to possibly edit the file (as indicated in the tutorial from the GDAL websites on how to open a file). The constant is an "int", but its "int" value seemed to be read from the "gdalconst_wrap.dll".
If I replace the last line of the code by
DataSource test = Ogr.Open(ConfigurationManager.AppSettings["pathOfShapefile"], 0);
The exception do not happen anymore, and the code seems to run. This works for any "int" value proposed as a second argument of the function, as far as I've tried. However, I do not know what is the effect of the choosen int value, and how the open function interprets it. I'll edit a second time if I find more about it.
I thus believe that something is wrong in the reading of the constant value in "gdalconst_wrap.dll", and that the NuGet package "GDAL" might put it in the wrong place to begin with.