2012-03-08 2 views
1

나는 C# service를 사용하여 사용자의 특정 폴더를 차단하려고합니다. 즉, 서비스가 차단되었거나 차단되었지만 서비스를 시작하면 최종 결과는 차단 된 폴더입니다. 나는 코드를 작성했다. 하지만 응답은 확실합니까? 당신은 CACLS 프로세스의 표준 입력을 리디렉션이C# windows 서비스에서 CACLS를 사용하는 방법

string Pathname = folder; 
EventLog.WriteEntry(Pathname); 
string Username = @"BUILTIN\Users"; 
string UserRights = "N"; 

if (Pathname == null || Pathname == "") 
{ 
    EventLog.WriteEntry("No File"); 
} 

string CommandLine = '"' + System.IO.Path.GetFullPath(Pathname) + '"' + " /C "; 
CommandLine += @" /T "; 
CommandLine += @" /P " + Username + ":" + UserRights; 

Process p = new Process(); 
p.StartInfo.FileName = "cacls.exe"; 
p.StartInfo.Arguments = CommandLine; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.RedirectStandardInput = true; 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; 
p.StartInfo.CreateNoWindow = true; 

p.Start(); 

string Response = p.StandardOutput.ReadToEnd(); 
EventLog.WriteEntry(Response); 
if (Response == null || Response == "") 
{ 
    EventLog.WriteEntry("Error"); 
} 

답변

1

를 해결하는 데 도움이 바랍니다. 사용하는 대신, 명령 줄 cacls 유틸리티를 호출하지 마십시오이 sample from MSDN

StreamWriter myStreamWriter = myProcess.StandardInput; 
myStreamWriter.Writeline("Y"); // Could it be localized ??? -->Oui, Ja, Sì etc... 
myStreamWriter.Close(); // This seems to be is important..... 
+0

나는 p.StandardInput.WriteLine ("Y")을 시도했다. 귀하의 방법, 둘 다 작동하지 않습니다 – lankitha

+0

나는 내 대답을 업데이 트했습니다 – Steve

5

에서
봐, (A stream.Writeline ("Y")처럼)
그래서 당신은 당신의 입력과 프로세스를 공급합니다 사용 권한을 변경하는 .NET API 명령 줄 유틸리티 호출은 작업 수행을위한 마지막 해결 방법으로 항상 보여야합니다. 프로그래밍 방식 액세스를위한 API에 직접 액세스하는 것이 훨씬 좋습니다.

  • Directory.GetAccessControl(string)은 현재 ACL을 가져옵니다.
  • Directory.SetAccessControl(string, DirectorySecurity)은 ACL을 설정합니다.

일반적으로 ACL을 사용하여 작업 할 때는 권한을 부여하고 거부 플래그를 전혀 사용하지 않는 것이 좋습니다. 거부하는 BUILTIN\Users은 매우 광범위하며 거부하면 사용자에게 부여 된 모든 권한이 에 위배됩니다. 일반 사용자에게 권한을 부여하지 않고 액세스 권한이있는 특정 사용자에게만 권한을 부여하는 ACL을 구성하는 것이 좋습니다.