回答編集履歴

1

サンプルコードを書いてみました

2016/09/28 02:54

投稿

htsign
htsign

スコア870

test CHANGED
@@ -1,3 +1,55 @@
1
1
  WQLで[Win32_LocalTime](http://www.wmifun.net/library/win32_localtime.html)クラスを叩くことで取得できると思います。
2
2
 
3
3
  [リモートコンピュータの WMI にアクセスする方法](http://uchukamen.com/Programming/WMI/#SEC7)が参考になるかと思います。
4
+
5
+
6
+
7
+ ```c#
8
+
9
+ class Sample
10
+
11
+ {
12
+
13
+ static void Main(string[] args)
14
+
15
+ {
16
+
17
+ const string ServerName = "server";
18
+
19
+ const string UserName = "user";
20
+
21
+ const string Password = "pass";
22
+
23
+
24
+
25
+ var option = new ConnectionOptions { Username = UserName, Password = Password };
26
+
27
+ var scope = new ManagementScope(string.Format(@"\\{0}\root\cimv2", ServerName), option);
28
+
29
+ var searcher = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM Win32_LocalTime"));
30
+
31
+ ManagementObjectCollection collection = searcher.Get();
32
+
33
+ foreach (ManagementObject mo in collection)
34
+
35
+ {
36
+
37
+ Console.WriteLine(string.Format("{0:D4}年{1:D2}月{2:D2}日({3}) {4:D2}時{5:D2}分{6:D2}秒",
38
+
39
+ mo["Year"], mo["Month"], mo["Day"], (DayOfWeek)(uint)mo["DayOfWeek"],
40
+
41
+ mo["Hour"], mo["Minute"], mo["Second"]));
42
+
43
+ }
44
+
45
+ Console.ReadLine();
46
+
47
+ }
48
+
49
+ }
50
+
51
+ ```
52
+
53
+ 手元の環境的な問題でリモートマシンへのアクセスはテストしていませんが、ローカルの場合(`searcher`に`scope`を渡さない場合)に時刻が取れるところまでは確認しました。
54
+
55
+ 参照設定で`System.Management.dll`を追加する必要があります。