private static void SetVariable(string fullpath, string value, bool isRegExpandSz) { object objValue = value; object objType = (isRegExpandSz) ? "REG_EXPAND_SZ" : "REG_SZ"; WshShell shell = new WshShell(); shell.RegWrite(fullpath, ref objValue, ref objType); // need to call BroadcastChange() to modify registry }
// // Thank you Greg Houston for a good solution // http://ghouston.blogspot.com/2005/08/how-to-create-and-change-environment.html /// <summary> /// Sets the variable. /// </summary> /// <param name="fullpath">The fullpath.</param> /// <param name="value">The value.</param> /// <param name="isRegExpandSz">if set to <c>true</c> [is reg expand sz].</param> private void SetVariable(string fullpath, string value, bool isRegExpandSz) { object objValue = value; object objType = (isRegExpandSz) ? "REG_EXPAND_SZ" : "REG_SZ"; shell.RegWrite(fullpath, ref objValue, ref objType); int result; SendMessageTimeout((System.IntPtr)HWND_BROADCAST, WM_SETTINGCHANGE, 0, "Environment", SMTO_BLOCK | SMTO_ABORTIFHUNG | SMTO_NOTIMEOUTIFNOTHUNG, 5000, out result); }