2014-03-27 145 views
0

我一直在尋找如何獲得遠程PC的System.Environment.TickCount。 使用這個簡單的代碼可以從我的本地PC獲取我想要的信息,但我無法弄清楚如何爲我們的域網中的每臺PC獲取相同的信息。我想從我們的服務器運行。獲取網絡計算機System.Environment.TickCount

TimeSpan t = TimeSpan.FromMilliseconds(System.Environment.TickCount); 
MessageBox.Show(t.Days.ToString() + "days, " + t.Hours.ToString() + "hrs & " + t.Minutes.ToString() + "mins."); 

我有了這個代碼來獲取網絡中的所有計算機的名稱:

public List<String> ListNetworkComputers() 
{ 
    List<String> _ComputerNames = new List<String>(); 
    String _ComputerSchema = "Computer"; 
    System.DirectoryServices.DirectoryEntry _WinNTDirectoryEntries = new System.DirectoryServices.DirectoryEntry("WinNT:"); 
    foreach (System.DirectoryServices.DirectoryEntry _AvailDomains in _WinNTDirectoryEntries.Children) 
    { 
     foreach (System.DirectoryServices.DirectoryEntry _PCNameEntry in _AvailDomains.Children) 
     { 
      if (_PCNameEntry.SchemaClassName.ToLower().Contains(_ComputerSchema.ToLower())) 
      { 
       _ComputerNames.Add(_PCNameEntry.Name); 
      } 
     } 
    } 
    return _ComputerNames; 
} 

如何使用這些信息來獲得從每臺PC的System.Environment.TickCount? 我試過PsExec.exe,但我真的沒有線索如何讓它爲我工作。我試過這個,但它不起作用:

var list = ListNetworkComputers(); 
foreach (var pc in list) 
{ 
    string output = ""; 
    using (var process = new System.Diagnostics.Process()) 
    { 
     process.StartInfo.FileName = @"C:\PsExec.exe"; 
     process.StartInfo.Arguments = @"\\" + pc + " cmd /c echo " + "System.Environment.TickCount"; 
     process.StartInfo.UseShellExecute = false; 
     process.StartInfo.CreateNoWindow = true; 
     process.StartInfo.RedirectStandardOutput = true; 
     process.Start(); 
     output = process.StandardOutput.ReadToEnd(); 
    } 
    int count = 0; 
    Int32.TryParse(output, out count); 
    TimeSpan ts = TimeSpan.FromMilliseconds(count); 
    MessageBox.Show(pc + ": " + ts.Days.ToString() + "days, " + ts.Hours.ToString() + "hrs & " + ts.Minutes.ToString() + "mins."); 
} 
+0

如果你想同步時鐘,你可以看看運行本地NTP服務器。 – Blorgbeard

+0

我只是想知道每臺PC已經運行了多長時間。 – Mutley

+0

http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem –

回答

0

而不是使用「cmd.exe」,也許你可以使用PowerShell?如果是這樣,這是一個簡單的命令來打印該屬性:[System.Environment]::TickCount

+0

如何在我的應用程序中使用PowerShell?我沒有經驗。 – Mutley