private void add_Click(object sender, EventArgs e) { AddRoute form = new AddRoute(); DialogResult result = form.ShowDialog(this); if (result != DialogResult.Cancel) { using (PowerShell PowerShellInstance = PowerShell.Create()) { string nextHop = string.Empty; if (!string.IsNullOrEmpty(form.NextHop)) { nextHop = "-NextHop " + form.NextHop; } string persistent = string.Empty; if (!form.Persistent) { // Do not specify PolicyStore for PersistentStore persistent = "-PolicyStore ActiveStore"; } string format = "New-NetRoute -DestinationPrefix {0} -InterfaceIndex {1} {2} {3} -RouteMetric {4}"; string script = string.Format(format, form.DestinationPrefix, form.InterfaceIndex, nextHop, persistent, form.RouteMetric); PowerShellInstance.AddScript(script); PowerShellInstance.Invoke(); } RefreshRoutes(); } }