Die in C# übersetzte Version von diesem Snippet von Volker Steitz:
http://www.dotnet-snippets.de/dns/vbnet-anwendung-unter-xpvista-als-admin-starten-SID407.aspx
Folgender Code muss noch eingefügt werden:
Zu den Using-Anweisungen:
using System.Security;
In den Konstruktor:
txtUser.Text = System.Environment.UserName;
private System.Diagnostics.Process StartNewProcessWithAdminCredentials(string executablePathAndName, string sUsername, string startArguments)
{
System.Diagnostics.ProcessStartInfo newProcessStartUpInfo;
try
{
if (startArguments != null)
{
newProcessStartUpInfo = new System.Diagnostics.ProcessStartInfo(executablePathAndName, startArguments);
}
else
{
newProcessStartUpInfo = new System.Diagnostics.ProcessStartInfo(executablePathAndName);
}
// Betriebsystem prüfen: XP oder höher (Vista = 6)
if (System.Environment.OSVersion.Version.Major >= 5)
{
newProcessStartUpInfo.Verb = "runas";
newProcessStartUpInfo.UseShellExecute = false;
newProcessStartUpInfo.Password = password;
newProcessStartUpInfo.UserName = sUsername;
return System.Diagnostics.Process.Start(newProcessStartUpInfo);
}
else
{
return System.Diagnostics.Process.Start(newProcessStartUpInfo);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
private void Button1_Click(object sender, EventArgs e)
{
this.StartNewProcessWithAdminCredentials(@"Pfad der zu startenden Anwendung", txtUser.Text, null);
}
private void txtPass_Enter(object sender, EventArgs e)
{
password.Clear();
}
private void txtPass_KeyPress(object sender, KeyPressEventArgs e)
{
password.AppendChar(e.KeyChar);
}
Kommentare zum Snippet