| 11 | |
| 12 | ----- |
| 13 | === Q & A === |
| 14 | |
| 15 | ==== Modifying Local Search Path ==== |
| 16 | > Can I set a system path from C# that will be searched and that |
| 17 | > will not interfere with possible other processes search path? |
| 18 | |
| 19 | |
| 20 | If you want to add FWTools bin folder to PATH during run-time, so you don't have to pollute system PATH permanently, |
| 21 | you can do it this way, in C# |
| 22 | {{{ |
| 23 | DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)] |
| 24 | public static extern bool |
| 25 | SetEnvironmentVariable(string lpName, string lpValue); |
| 26 | |
| 27 | string path = Environment.GetEnvironmentVariable("PATH"); |
| 28 | path += @";C:\Program Files\FWTools\bin"; |
| 29 | SetEnvironmentVariable("PATH", path); |
| 30 | }}} |
| 31 | Now, for current process, all DLLs from FWTools package are accessible. |
| 32 | |
| 33 | MSDN documentation: |
| 34 | * http://msdn2.microsoft.com/en-us/library/ms686206.aspx |
| 35 | * http://msdn2.microsoft.com/en-us/library/system.environment.setenvironmentvariable.aspx |
| 36 | |
| 37 | Instead of the `P/Invoke` call to `SetEnvironmentVariable`, you |
| 38 | can use C# native method `Environment.SetEnvironmentVariable` (AFAIR, |
| 39 | available from .NET >= 2.0). |
| 40 | Read the doc carefully, because there are two versions of this method. |
| 41 | Unlike the Win32 API call accessed through `P/Invoke`, the method |
| 42 | `Environment.SetEnvironmentVariable` has overloaded version that *may* |
| 43 | change environment permanently, across processes. |
| 44 | |
| 45 | ''adapted from http://lists.maptools.org/pipermail/gdal-dev/2007-August/013823.html'' |