1

I have referenced some other topics and it comes up the following coding, yet it doesn't work. I allow it creates command window and use "/k" argument keeping the window opened so that I can trace its output.

However, I can see the warning from the window that the command needs admin rights to execute. How can I execute it in admin rights?

public void ClearArpCache () {
        ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe", "/user:Administrator \" cmd /k arp -d \""); // same as "netsh interface ip delete arpcache"
        processStartInfo.RedirectStandardOutput = true;
        //processStartInfo.CreateNoWindow = true;
        //processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        processStartInfo.UseShellExecute = false;
        processStartInfo.StandardOutputEncoding = Encoding.Default;
        processStartInfo.Verb = "runas";

        Process.Start (processStartInfo);
        UnityEngine.Debug.Log ("ARP table cache cleared.");
    }

EDITED:

Try changing from "cmd.exe" to "runas.exe"

public void ClearArpCache () {
        ProcessStartInfo processStartInfo = new ProcessStartInfo("runas.exe", "/user:Administrator \" cmd /k arp -d \""); // same as "netsh interface ip delete arpcache"
        processStartInfo.RedirectStandardOutput = true;
        //processStartInfo.CreateNoWindow = true;
        //processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        processStartInfo.UseShellExecute = false;
        processStartInfo.StandardOutputEncoding = Encoding.Default;

        Process.Start (processStartInfo);
        UnityEngine.Debug.Log ("ARP table cache cleared.");
    }
Simon Leung
  • 109
  • 3
  • 14

1 Answers1

1

Just clarified why it failed using "runas". In order to use "runas" in Verb, UseShellExecute must be firstly turned true.

When executing following function, it will pop a window asking for admin rights, just click 'Yes' to begin the execution. Though it is beyond the scope, if possible I want to skip the pop-up window as well.

public void ClearArpCache () {
        ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe", "/k arp -d"); // same as "netsh interface ip delete arpcache"
        processStartInfo.CreateNoWindow = true;
        processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        processStartInfo.UseShellExecute = true;
        processStartInfo.Verb = "runas";

        Process.Start (processStartInfo);
        UnityEngine.Debug.Log ("ARP table cache cleared.");
    }
Simon Leung
  • 109
  • 3
  • 14
  • If you don't like to get the UAC popup, you have to [disable it](https://www.howtogeek.com/howto/windows-vista/disable-user-account-control-uac-the-easy-way-on-windows-vista/). – Oliver Aug 22 '17 at 12:32