2015-01-20 3 views
0

C#을 사용하여 VS2012에 내장 한 콘솔 앱이 있습니다. EXE는 공유 드라이브에 있으며 모든 사용자가 실행할 수 있지만 항상 EXE가 AD에 설정된 특정 시스템 계정을 사용하여 실행되기를 원합니다. 본질적으로, EXE를 마우스 오른쪽 단추로 클릭하고 "Run As ..."을 수행하는 프로그래밍 방식으로 모방하려고합니다.특정 계정을 사용하여 콘솔 앱을 강제 실행하십시오.

코드가 특정 계정/암호로 항상 실행되도록하려면 어떻게해야합니까?

+0

확인이 답변 http://stackoverflow.com/questions/2532769/how-to-start-a-process-as-administrator-mode-in-c-sharp –

+0

), 그것은 당신을 도울 수 있기를 바랍니다 웹상에 예제가 많이 있습니다.이 작업을 수행하는 방법에 대한 많은 예제가 있습니다. 여기서는'ProcessStartInfo' 클래스에 익숙합니다. 여기서는 RunUnderSpecific User라는 메소드를 호출 할 수 있습니다. 그런 다음 사용자 이름과 비밀번호를 ProcessStartInfo 객체에 전달하십시오. .. [다른 사용자의 자격 증명으로 프로세스 시작] (http://stackoverflow.com/questions/6413900/launch-a-process-under-another-users-credentials) – MethodMan

+0

@MethodMan - 그렇게 생각할 수도 있지만 내가 찾을 수있는 네 가지 Google 검색은 특정 계정이 아닌 관리자로 실행되는 참조 또는 현재 사용자 ID를 찾는 것입니다. 당신이 제공 한 두 번째 링크는 유망 해 보입니다. 감사! – Omegacron

답변

1

내 앱 중 하나에 이것을 썼습니다.

public partial class App : Application 
{ 
    protected override void OnStartup(StartupEventArgs e) 
    { 
     // Launch itself as administrator 
     ProcessStartInfo proc = new ProcessStartInfo(); 

     // this parameter is very important 
     proc.UseShellExecute = true; 

     proc.WorkingDirectory = Environment.CurrentDirectory; 
     proc.FileName = Assembly.GetEntryAssembly().Location; 

     // optional. Depend on your app 
     proc.Arguments = this.GetCommandLine(); 

     proc.Verb = "runas"; 
     proc.UserName = "XXXX"; 
     proc.Password = "XXXX"; 

     try 
     { 
      Process elevatedProcess = Process.Start(proc); 

      elevatedProcess.WaitForExit(); 
      exitCode = elevatedProcess.ExitCode; 
     } 
     catch 
     { 
      // The user refused the elevation. 
      // Do nothing and return directly ... 
      exitCode = -1; 
     } 

     // Quit itself 
     Environment.Exit(exitCode); 
     } 
    } 
} 
+0

나는 메시지를 으로받습니다. 프로세스 객체는 사용자로 프로세스를 시작하기 위해 UseShellExecute 속성을 false로 설정해야합니다. – ProgSky

관련 문제