/// <summary> /// タイマー実行される関数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { LoadRegistry(); lock (LockObject) { if (DateTime.Now > NextExecutionDateTime) { bool fnetworkCapability = true; try { // ホスト名取得 HostName = Dns.GetHostName(); // IP取得 IpAddress = GetIpFromHost(HostName); // Hostsのフォーマットに変換 HostsLine = IpAddress + " " + HostName; } catch(Exception) { fnetworkCapability = false; eventLog.WriteEntry("Cannot get the Hostname and IP.", EventLogEntryType.Error); } if (fnetworkCapability) { foreach (string strExternalHostsSaveTarget in externalHostsSaveTargetCollection) { // 外部Hostsがネットワーク上か調べる string externalHostsSaveTargetFolderPath = Path.GetDirectoryName(strExternalHostsSaveTarget); NetworkUtility networkUtility = new NetworkUtility(externalHostsSaveTargetFolderPath); // 外部Hostsがネットワーク上だった場合 if (networkUtility.CheckNetworkPath()) { eventLog.WriteEntry(externalHostsSaveTargetFolderPath + " is network path.", EventLogEntryType.Information); // ユーザーを偽装する using (NativeMethods.GetImpersonationContext("NetworkService", "NT AUTHORITY", "")) { bool isSuccessNetworkConnect; // ユーザー情報に応じて接続を試みる if (IsNetworkAuthentication) { isSuccessNetworkConnect = networkUtility.Connect(externalHostsSaveTargetFolderPath, NetworkAuthenticationUsername, NetworkAuthenticationPassword); } else { isSuccessNetworkConnect = networkUtility.Connect(); } // 接続成功の場合 if (isSuccessNetworkConnect) { eventLog.WriteEntry("Succeeded connecting to " + externalHostsSaveTargetFolderPath, EventLogEntryType.Information); // Hostsをアップデート UpdateHosts(strExternalHostsSaveTarget); // ネットワーク切断と破棄 if (isSuccessNetworkConnect) { networkUtility.Disconnect(); eventLog.WriteEntry("Disconnected to " + externalHostsSaveTargetFolderPath, EventLogEntryType.Information); } networkUtility = null; } // 接続失敗の場合 else { eventLog.WriteEntry("Failed connecting to " + externalHostsSaveTargetFolderPath, EventLogEntryType.Information); } } } // 外部Hostsがローカルだった場合 else { eventLog.WriteEntry(externalHostsSaveTargetFolderPath + " is local path.", EventLogEntryType.Information); // Hostsをアップデート UpdateHosts(strExternalHostsSaveTarget); } } } UpdateNextPoll(); } } }