JavaScript WScript.Shell.Run 需在系統 PATH 中找到執行檔。安裝時將應用程式目錄加入系統環境變數 PATH。
[RunInstaller(true)]
public class InstallAction : Installer
{
private string virtualRoot = string.Empty;
private string physicalRoot = string.Empty;
private Container components = null;
public InstallAction() { InitializeComponent(); }
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
try
{
// ... 其他安裝邏輯 ...
UpdatePathEnvironment(); // 修改 PATH
}
catch (Exception ex)
{
WriteLog(ex.Message + "\r\n" + ex.StackTrace);
}
}
private void UpdatePathEnvironment()
{
string registerKey = "SYSTEM\\ControlSet001\\Control\\Session Manager\\Environment";
string key = "Path";
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(registerKey);
string result = regKey.GetValue(key).ToString();
// 避免重複添加
if (result.IndexOf(physicalRoot) < 0)
{
result += string.Format(";{0}", physicalRoot);
}
// 需寫入權限重新開啟
regKey = Registry.LocalMachine.OpenSubKey(
registerKey,
RegistryKeyPermissionCheck.ReadWriteSubTree,
RegistryRights.SetValue);
regKey.SetValue(key, result);
}
// ... 其他方法 ...
}
SYSTEM\ControlSet001\Control\Session Manager\Environment\PathRegistryKeyPermissionCheck.ReadWriteSubTree + RegistryRights.SetValueIndexOf 檢查避免重複路徑; 分隔