2016-11-28 10 views
-1

net use 명령을 실행 한 후에 디렉토리가 존재하는지 테스트하려했지만 mapDrive()가 드라이브 매핑을 완료하기 전에 checkMappedDrive()가 실행 중입니다.드라이브가 C#에서 net use로 성공적으로 매핑되었는지 어떻게 확인할 수 있습니까?

public void mapDrive(String driveChar, String server, String user, String password){ 
    String path = "use "+driveChar+": "+server +" /user:"+user+ " "+password; 
    proc.StartInfo.FileName = "net"; 
    proc.StartInfo.Arguments = path; 
    proc.StartInfo.UseShellExecute = false; 
    proc.Start(); 

    if(checkMappedDrive(driveChar)){ 
     //nice 
    }else{ 
     //error 
    } 

} 

public bool checkMappedDrive(String driveChar){ 

    String drive = Path.GetPathRoot(driveChar.ToUpper()+":\\"); 
    Debug.WriteLine("Checking: " + drive); 
    if (!Directory.Exists(drive)){ 
      proc.Kill(); 
      //bad 
    return false; 
    } 
     //nice 
    return true; 
} 
+0

사용 Process.WaitForExit() 쉘 작업이 완료되었는지 확인하기 . 그리고 프로세스의 ExitCode에서 오류가 있는지 확인하십시오. 그 후에도 폴더가 성공적으로 매핑되었는지 확인해야 할 수도 있습니다. –

+0

사용자가 유효하지 않을 때 프로세스를 완료하는 데 약 10-15 초가 걸렸으므로 서버가 과부하 상태 일 수 있으므로 프로세스를 완료하고 싶습니다. – tomyforever

답변

1

당신은 Process.WaitforExit 사용할 수 있습니다

public void mapDrive(String driveChar, String server, String user, String password){ 
    String path = "use "+driveChar+": "+server +" /user:"+user+ " "+password; 
    proc.StartInfo.FileName = "net"; 
    proc.StartInfo.Arguments = path; 
    proc.StartInfo.UseShellExecute = false; 
    proc.Start(); 
    proc.WaitForExit(10000); // wait 10 seconds at a maximum 

    if(checkMappedDrive(driveChar)){ 
     //nice 
    }else{ 
     //error 
    } 

} 
+0

시간 초과가있는 매개 변수를 매개 변수로 사용하는 것을 잊지 마십시오. https://msdn.microsoft.com/en-us/library/ty0d8k56.aspx – RvdK

+0

그래서 몇 초만 기다리는 것이 유일한 방법입니까?/ – tomyforever

+0

@tomyforever : 메서드는 x 초를 기다리지 않고 프로세스가 존재할 때까지 기다릴 것이지만 대기 시간이 x-milliseconds (10 초 이상)를 초과 할 경우 기다릴 것입니다. 잇다. 따라서'Process.WaitForExit'에 대한 매개 변수는 드라이브를 매핑하는 데 보통 필요한 양의 두 배가되어야합니다. –