2017-08-27 3 views
-1

J'ai quelques commandes que je veux exécuter via le CMD (admin): ipconfig/flushdns ipconfig /registerdns ipconfig /release ipconfig /renew netsh winsock reset Quelqu'un peut me dire comment?Comment exécuter certaines commandes CMD une par une (en tant qu'admin)

+1

Copie possible de [Elevating process privilege programatically?] (Https://stackoverflow.com/questions/133379/elevating-process-privilege-programmatically) – Vanna

Répondre

0

Question de réponse est un peu ici: Running cmd commands with Administrator rights

Le code pourrait être dupliqué (comme indiqué ci-dessous):

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 ipconfig /flushdns"; 
    startInfo.Verb = "runas"; 
    process.StartInfo = startInfo; 
    process.Start(); 

System.Diagnostics.Process process2 = new System.Diagnostics.Process(); 
    System.Diagnostics.ProcessStartInfo startInfo2 = new 
    System.Diagnostics.ProcessStartInfo(); 
    startInfo2.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
    startInfo2.FileName = "cmd.exe"; 
    startInfo2.Arguments = "/C ipconfig /renew"; 
    startInfo2.Verb = "runas"; 
    process2.StartInfo = startInfo2; 
    process2.Start(); 
+0

Ceci exécute les commandes CMD en tant qu'administrateur, n'est-ce pas? (même si l'utilisateur n'a pas choisi "Exécuter en tant qu'administrateur") – Leviathan

+0

Et il y a une erreur sur: process2.StartInfo2 = startInfo2; – Leviathan

+1

Doit être process2.StartInfo = startInfo2 – JSWulf

0

Si vous cherchez à utiliser powershell, vous pouvez créer un script qui comprendra vos commandes quelque chose comme ceci:

& "command 1"; & "command 2"; etc.