24 Şubat 2015 Salı

c# administrator yetkisi kontrol

if (IsAdministrator() == false)
{
    // Restart program and run as admin
    var exeName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
    ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
    startInfo.Verb = "runas";
    System.Diagnostics.Process.Start(startInfo);
    Application.Current.Shutdown();
    return;
}

private static bool IsAdministrator()
{
    WindowsIdentity identity = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(identity);
    return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
---------------------

var processStartInfo = new ProcessStartInfo(@"C:\Windows\Notepad.exe");
processStartInfo.Verb = "runas";
try
{
    Process.Start(processStartInfo);
}
catch (Win32Exception ex)
{
    MessageBox.Show(ex.ToString(), "Run As",
        MessageBoxButtons.OK, MessageBoxIcon.Error);

}



---------------------

if (Environment.OSVersion.Version.Major >= 6)
{
    processStartInfo.Verb = "runas";
}

Hiç yorum yok:

Yorum Gönder