{"id":1006,"date":"2013-01-05T09:29:37","date_gmt":"2013-01-05T08:29:37","guid":{"rendered":"https:\/\/www.kolja-engelmann.de\/blog\/?p=1006"},"modified":"2013-01-05T12:53:27","modified_gmt":"2013-01-05T11:53:27","slug":"c-programm-automatisch-mit-administratorrechten-neu-starten","status":"publish","type":"post","link":"https:\/\/www.kolja-engelmann.de\/blog\/2013\/01\/c-programm-automatisch-mit-administratorrechten-neu-starten\/","title":{"rendered":"C# &#8211; Programm automatisch mit Administratorrechten neu starten"},"content":{"rendered":"<p>Da ich es gerade mal wieder brauchte: Wie erreicht man, dass sich ein Programm selbst mit Administratorrechten neu startet?<\/p>\n<p>Dazu zun\u00e4chst eine kleine Abfrage, ob man bereits \u00fcber entsprechende Berechtigungen verf\u00fcgt (Zeile 1-2)<br \/>\nDann setzt man den Pfad zur eigenen ausf\u00fchrbaren Datei zusammen (Zeile 5) <em>\/\/Hierf\u00fcr gibt&#8217;s wahrscheinlich 100 verschiedene M\u00f6glichkeiten<br \/>\n<\/em>Dann startet man sich selbst mit Adminrechten neu (Zeile 8)<br \/>\nUnd beendet abschlie\u00dfend das bereits laufende Programm (Zeile 11)<\/p>\n<pre class=\"lang:c# decode:true\">WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());\r\nif (principal.IsInRole(WindowsBuiltInRole.Administrator) == false)\r\n{\r\n\t\/\/Get the full qualified path to the own executable\r\n\tstring x = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;\r\n\r\n\t\/\/Start itself with adminrights\r\n\tif (RunElevated(x) == true)\r\n\t{\r\n\t\t\/\/Close old programwindow\r\n\t\tthis.Close();\r\n\t}\r\n}<\/pre>\n<p>Der eigentlich Start mit Adminrechten ist dann (abgesehen vom UAC Fenster) ziemlich unspektakul\u00e4r:<\/p>\n<pre class=\"lang:default decode:true\">\/\/\/ &lt;summary&gt;\r\n\/\/\/ Run a program with elevated rights\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;param name=\"fileName\"&gt;Path to the executable to run&lt;\/param&gt;\r\n\/\/\/ &lt;returns&gt;True if no error has occurred&lt;\/returns&gt;\r\nprivate static bool RunElevated(string fileName)\r\n{\r\n\tProcessStartInfo processInfo = new ProcessStartInfo();\r\n\tprocessInfo.Verb = \"runas\";\r\n\tprocessInfo.FileName = fileName;\r\n\ttry\r\n\t{\r\n\t\tProcess.Start(processInfo);\r\n\t\treturn true;\r\n\t}\r\n\tcatch (Win32Exception)\r\n\t{\r\n\t\t\/\/Do nothing. Probably the user canceled the UAC window\r\n\t}\r\n\treturn false;\r\n}<\/pre>\n<p>Done;<\/p>\n<p>Gibts noch bessere M\u00f6glichkeiten? Ich bin f\u00fcr alles offen (und schiele mal so Richtung Sven, der es sich zum Hobby gemacht hat mich zu korrigieren \ud83d\ude09 )<\/p>","protected":false},"excerpt":{"rendered":"<p>Da ich es gerade mal wieder brauchte: Wie erreicht man, dass sich ein Programm selbst mit Administratorrechten neu startet? Dazu zun\u00e4chst eine kleine Abfrage, ob man bereits \u00fcber entsprechende Berechtigungen verf\u00fcgt (Zeile 1-2) Dann setzt&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":867,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[38],"tags":[39],"class_list":["post-1006","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codeschnippsel","tag-c"],"jetpack_featured_media_url":"https:\/\/www.kolja-engelmann.de\/blog\/wp-content\/uploads\/2012\/11\/c_sourcecode.jpg","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/posts\/1006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/comments?post=1006"}],"version-history":[{"count":0,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/posts\/1006\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/media\/867"}],"wp:attachment":[{"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/media?parent=1006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/categories?post=1006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolja-engelmann.de\/blog\/wp-json\/wp\/v2\/tags?post=1006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}