none
how to set environment variable using C# code without rebooting the PC

    Question

  • Hi,

    From code how can I set environment variable for apache tomact (Catalina_Home and Path variable)?

    Till now I tried this(two ways), it setting the environment variable for tomcat but when I run startup.bat file of tomcat, giving error catalina_home not set. But when I restarted the PC it was working.

    Below is my code, what is wrong or what I am missing?

            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg,
                UIntPtr wParam, string lParam);

            private bool SetTomcatEnvironmentVariable()
            {
                try
                {
                    /*
                    //Set CATALINA_HOME in User variable
                    string EnvCATALINA_HOME = System.Environment.GetEnvironmentVariable("CATALINA_HOME", EnvironmentVariableTarget.Machine) ?? string.Empty; //EnvironmentVariableTarget.User
                    if (!string.IsNullOrEmpty(EnvCATALINA_HOME) && !EnvCATALINA_HOME.EndsWith(";"))
                        EnvCATALINA_HOME = EnvCATALINA_HOME + ';';
                    EnvCATALINA_HOME = EnvCATALINA_HOME + @"C:\apache-tomcat-8.0.33";
                    Environment.SetEnvironmentVariable("CATALINA_HOME", EnvCATALINA_HOME, EnvironmentVariableTarget.Machine);

                    //Set Path in System variable
                    string EnvPath = System.Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine) ?? string.Empty;
                    if (!string.IsNullOrEmpty(EnvPath) && !EnvPath.EndsWith(";"))
                        EnvPath = EnvPath + ';';
                    EnvPath = EnvPath + @"%CATALINA_HOME%\bin";
                    Environment.SetEnvironmentVariable("Path", EnvPath, EnvironmentVariableTarget.Machine);
                    */
                    const int HWND_BROADCAST = 0xffff;
                    const uint WM_SETTINGCHANGE = 0x001a;

                    using (var envKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true))
                    {
                        Contract.Assert(envKey != null, @"registry key is missing!");
                        string EnvCATALINA_HOME = System.Environment.GetEnvironmentVariable("CATALINA_HOME", EnvironmentVariableTarget.Machine) ?? string.Empty; //EnvironmentVariableTarget.User
                        if (string.IsNullOrEmpty(EnvCATALINA_HOME))
                        {    //EnvCATALINA_HOME = EnvCATALINA_HOME + ';';
                            EnvCATALINA_HOME = EnvCATALINA_HOME + @"C:\apache-tomcat-8.0.33";
                            envKey.SetValue("CATALINA_HOME", EnvCATALINA_HOME);
                            SendNotifyMessage((IntPtr)HWND_BROADCAST, WM_SETTINGCHANGE, (UIntPtr)0, "Environment");
                        }

                        string EnvPath = System.Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine) ?? string.Empty;
                        if (!string.IsNullOrEmpty(EnvPath) && !EnvPath.EndsWith(";"))
                            EnvPath = EnvPath + ';';
                        EnvPath = EnvPath + EnvCATALINA_HOME + @"\bin";//EnvPath + @"%CATALINA_HOME%\bin";
                        envKey.SetValue("Path", EnvPath);
                        SendNotifyMessage((IntPtr)HWND_BROADCAST, WM_SETTINGCHANGE, (UIntPtr)0, "Environment");

                    }


                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }

    Please help.

    Thanks.

    • Moved by Just KarlModerator Friday, September 30, 2016 8:15 PM Looking for the correct forum.
    Tuesday, September 20, 2016 4:25 PM

Answers

All replies

  • Hello,

    The 'Academic Initiatives - Technical Queries' forum is for posts Related to technical / coding / programming related issues as related to Microsoft's Academic Initiatives.

    As it's off-topic here, I am moving the question to the Where is the forum for... forum.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join('6D73646E5F6B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    Friday, September 30, 2016 8:13 PM
    Moderator
  • Hello,

    You might ask Apache.

    Otherwise, you might ask in the Visual C# Language forum on MSDN.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join('6D73646E5F6B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    Friday, September 30, 2016 8:14 PM
    Moderator