如何检查驱动器是否已成功映射到C#中的net use?

时间:2016-11-28 08:29:06

标签: c# drive net-use

我在执行net use命令后尝试测试该目录是否存在,但是在mapDrive()完成映射驱动之前,checkMappedDrive()正在执行。

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;
}

2 个答案:

答案 0 :(得分:2)

您可以使用Process.WaitforExit

Example.A.new Entry(context);

Arrays.stream(Example.values()).map(item -> item.new Entry(context)).toArray(Example.Entry[]::new)

答案 1 :(得分:0)

使用sharedPreferences = getApplicationContext().getSharedPreferences("pref", MODE_PRIVATE); editor = sharedPreferences.edit(); editor.putString("User_Name", f_name); editor.putString("User_Email", email_id); editor.apply(); 以等待该过程完成

MSDN: Process.WaitForExit Method