Search code examples
c#debuggingdllenvironment-variablespath-variables

How to set environment variable for C# debugging


I'm trying to load managed and native dll into my C# application. I want to set the PATH environment variable, so the application can find the dlls to be loaded. In C++ that's easy, but how can I do that in a C# project? (By the way I'm using VS2012, .NET, WPF)


Solution

  • Use Environment.SetEnvironmentVariable().

    string currentPath = Environment.GetEnvironmentVariable("path");
    Environment.SetEnvironmentVariable("path",currentPath + ";c:\path_to_libraries");
    

    Keep in mind that this will only be in scope for the current process. If you want to set a persistent environment variable (user or machine scope) use the Environment.SetEnvironmentVariable(string, string, EnvironmentVariableTarget) overload. See here for that reference.