2017-06-01 88 views
0

遠程關機命令不適用於Windows Embedded計算機。它對正常的Windows電腦工作正常。有什麼特別的,我們需要做的Windows嵌入式? 我想從我的C#程序發送以下命令。也試圖通過命令行。遠程關機命令不適用於Windows Embedded計算機

shutdown /s /f /m \\192.168.100.2 /t 5 /d u:0:0 /c "The Computer is shutting down" 

代碼看起來像下面

Process proc = new Process(); 
proc.EnableRaisingEvents = false; 
proc.StartInfo.UseShellExecute = false; 
proc.StartInfo.CreateNoWindow = true; 
proc.StartInfo.FileName = "shutdown.exe"; 
proc.StartInfo.UserName = adminName; 
proc.StartInfo.Password = adminPassword; 
proc.StartInfo.Domain = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName; 

proc.StartInfo.Arguments = string.Format(@" /s /f /m \\{0} /t 5 /d u:0:0 /c ""The computer is shutting down"" ", ipAddress); 
try 
{ 
    proc.Start(); 
} 
catch(Exception ex) 
{ 
     // log 
} 
+0

你的日誌會說什麼嗎? –

+0

您是否捕獲了該命令的輸出,以便您可以查看它是否給您提供錯誤消息,而不僅僅是「它沒有工作」? –

+0

如果從命令提示符執行關機命令並且根本不涉及C#代碼,它會工作嗎?我問的原因是,如果它不能從命令提示符工作,那麼這是[su]的一個問題,因爲在C#代碼中包裝問題不會使其成爲編程問題。 –

回答

0

命令行工具,沒有工作,我最後寫下面的代碼來關閉計算機遠程。但是該代碼也適用於本地計算機。

public void ShutdownRemotePC(string ipAddress, string adminName, string adminPassword) 
     { 
      try 
      { 
       var query = new SelectQuery("Win32_OperatingSystem"); 

       // create always a new management scope 
       // create a default one and get immediate info if connection is be possible 
       ConnectionOptions connectionOptions = new ConnectionOptions 
       { 
        Impersonation = ImpersonationLevel.Impersonate, 
        EnablePrivileges = true, 
        //changed to packet privacy as some service requires it 
        Authentication = AuthenticationLevel.PacketPrivacy, 
        Username = ipAddress + @"\" + adminName, 
        Password = adminPassword, 
        Timeout = TimeSpan.FromMilliseconds(5000) 
       }; 
       string name = @"\\" + ipAddress + @"\root\cimv2"; 
       ManagementScope managementScope = new ManagementScope(name, connectionOptions); 

       // if already connected is checked inside connect 
       managementScope.Connect(); 
       if(!managementScope.IsConnected) 
       { 
        //Shutdown Failed. Managment Scope could not be connected. 
        return; 
       } 

       using(var searcher = new ManagementObjectSearcher(managementScope, query)) 
       { 
        // impersonate the searcher if not allready done 
        searcher.Scope.Options.Impersonation = System.Management.ImpersonationLevel.Impersonate; 
        searcher.Scope.Options.EnablePrivileges = true; 

        using(ManagementObjectCollection found = searcher.Get()) 
        { 
         foreach(ManagementObject os in found) 
         { 
          // Obtain in-parameters for the method 
          using(ManagementBaseObject inParams = os.GetMethodParameters("Win32Shutdown")) 
          { 

           // Add the input parameters. 
           inParams["Flags"] = 12; 

           // Execute the method and obtain the return values. 
           using(ManagementBaseObject outParams = os.InvokeMethod("Win32Shutdown", inParams, null)) 
           { 
            if(outParams != null) 
            { 
             var result = Convert.ToInt32(outParams["returnValue"]); 
             if(result == 0) 
             { 
              Logger.LogError("Shutdown successfully."); 
             } 

            } 
           } 
          } 
         } 
        } 
       } 
      } 
      catch(Exception ex) 
      { 
       // Shutdown PC=failed. 
      } 
     }