C#
2018.07.17 14:30
관리자 권한으로 실행하기
다물칸 주소복사
조회 수 205 추천 수 0 댓글 0
구분 | 팁&트릭 |
---|---|
출처 | http://www.gisdeveloper.co.kr/?cat=118 |
다음 함수 추가
-
public static bool IsAdministrator()
-
{
-
WindowsIdentity identity = WindowsIdentity.GetCurrent();
-
-
if (null != identity)
-
{
-
WindowsPrincipal principal = new WindowsPrincipal(identity);
-
return principal.IsInRole(WindowsBuiltInRole.Administrator);
-
}
-
-
return false;
-
}
program.cs에 다음코드 추가
-
if (IsAdministrator() == false)
-
{
-
try
-
{
-
ProcessStartInfo procInfo = new ProcessStartInfo();
-
procInfo.UseShellExecute = true;
-
procInfo.FileName = Application.ExecutablePath;
-
procInfo.WorkingDirectory = Environment.CurrentDirectory;
-
procInfo.Verb = "runas";
-
Process.Start(procInfo);
-
}
-
catch (Exception ex)
-
{
-
MessageBox.Show(ex.Message.ToString());
-
}
-
-
return;
-
}