0

I'm developing an application web with ASP.NET/C# and I would in a button importing the shapefiles into PostGIS, and I'm trying with this code, this code I'm using it in cmd.exe in Windows it imported but in the application no but it can't import the shapefiles and it can't send me what'is the error, can you have idea?

if (strFileType == ".shp")
    {
    txtFilePath.SaveAs(Server.MapPath("~/UploadedShap/" + strFileName + strFileType));
    string strNewPath = Server.MapPath("~/Uploadedshap/" + strFileName + strFileType);

    // Connection String to shapefiles
    string[] strCmdText = {"cd ..","cd ..","cd program files","cd postgresql","cd 9.4","cd bin","shp2pgsql -s 3857 '"+ strNewPath+ "' public.COMGEO | psql -h localhost -d is -U postgres -p 5432 -w alami"};
    System.Diagnostics.Process.Start("CMD.exe",String.Join(" ",strCmdText));
    }
  • You could try a pipe (microsoft.com/resources/documentation/windows/xp/all/proddocs/…) in the string for strCmdText to capture the output text.. The hard part of shell execute is that you can't see what happens in the window as it flashes up and is gone too quickly; other methods don't even show the window. To set the system environment variables as alexGis states use SetEnvironmentVariable msdn.microsoft.com/en-us/library/z46c489x(v=vs.110).aspx (this has often worked for me) – Michael Stimson Mar 21 '16 at 3:07
  • can you rectifay in the code i don't understand you – malyon Mar 21 '16 at 7:42
  • "shp2pgsql -s 3857 '"+ strNewPath+ "' public.COMGEO | psql -h localhost -d i2s -U postgres -p 5432 -w alami > C:\\DEBUG.TXT" will (probably) capture the output of the operation to file c:\Debug.txt so you can see any error messages with notepad. Often, as I have said, I need to set environment variables for the process when using GDAL/OGR; the environment variables for the cmd process should be the same as the program that spawned it, try alexGis answer also. – Michael Stimson Mar 21 '16 at 21:42
  • can you explain me, i'm using this but it can't do it "string strCmdText; strCmdText = @"shp2pgsql -s 3857 '"+ strNewPath+ "' public.COMGEO | psql -h localhost -d i2s -U postgres -p 5432 -w alami > C:\\DEBUG.TXT"; System.Diagnostics.Process.Start(@"C:\Program Files\PostgreSQL\9.4\bin\CMD.exe", strCmdText); " – malyon Mar 21 '16 at 21:55
  • What error message (if any) are you getting? Is the command being run but no output? What happens when you start a command window (windows + r, then type cmd) and paste that text into it with the value for strNewPath filled in. Does strNewPath contain spaces? Is c:\debug.txt being created, if so what's in that file? Can you try to run the command like stackoverflow.com/questions/206323/… ? – Michael Stimson Mar 21 '16 at 22:16
1

You need to set your operating system PATH variable to include the directory "c:\programs\postgresql\9.4\bin"

There should be no need to tell your OS where the command "shp2pgsql" lives inside your code, the PATH variable should do that. Navigating to the variable from some previous unknown directory makes for very fragile code.

  • or I have to add that path – malyon Mar 21 '16 at 7:22
  • i'm trying by this but it can't resolve the problem string[] strCmdText = {"shp2pgsql -s 3857 '"+ strNewPath+ "' public.COMGEO | psql -h localhost -d i2s -U postgres -p 5432 -w alami"}; System.Diagnostics.Process.Start(@"C:\Program Files\PostgreSQL\9.4\bin\CMD.exe",String.Join(" ",strCmdText)); – malyon Mar 21 '16 at 11:16
  • #alexGIS it can't compiled can you help me – malyon Mar 21 '16 at 16:08
  • You don't need the path to CMD.exe, your OS already knows that. You need specify the path to shp2pgsql. – alexGIS Mar 21 '16 at 16:48
  • #alexGIS but how i can do it can you help me please i(m doing this command (string strCmdText; strCmdText = @"shp2pgsql -s 3857 c:\comgeo.shp public.COMGEO | psql -h localhost -d i2s -U postgres -p 5432 -w alami"; System.Diagnostics.Process.Start(@"C:\Program Files\PostgreSQL\9.4\bin\CMD.exe", strCmdText);) – malyon Mar 21 '16 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.