Hi @Peter_1985 , Welcome to Microsoft Q&A,
Append "/c" to the command line argument, which instructs Command Prompt to execute the command and then terminate.
Please ensure the following:
- The application itself must have sufficient permissions to run the
netsh
command. This usually requires running the application as an administrator. - The
netsh
command infld1
must be a valid and correctly formatted command. Please make sure that the commands infld1
can be executed correctly in the command prompt. - If the user cancels the prompt for administrator privileges, the
catch
** block will catch theSystem.ComponentModel.Win32Exception
exception, and you can add appropriate handling code there.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c " + fld1; // Use "/c" to run the command and then terminate
startInfo.Verb = "runas";
process.StartInfo = startInfo;
try
{
process.Start();
process.WaitForExit();
}
catch (System.ComponentModel.Win32Exception ex)
{
// Handle exceptions, e.g., if the user cancels the elevation prompt
// MessageBox.Show(ex.Message);
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.