< 別アセンブリ内のリソースを参照する | 'IValueConverter' 型にはパブリック TypeConverter クラスがありません。 >

October 28, 2008

ネットワーク情報をいろいろ取得するやつ。

作ってからだいぶたつんですが、Windows 上のネットワーク環境の情報を.NET Framework を使用してごちゃごちゃ取ってきてどばっと表示するものです。こうやって見ると、Framework の階層構造がややこしいことがよくわかりますね。

<!-- Window1.xaml -->
<Window x:Class="NetworkInfomationSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:NetworkInfomationSample"
    Title="ネットワーク情報を取得するサンプル">
    <Window.Resources>
        <local:NetworkInfo x:Key="networkInfo"/>
        <local:InterfacePhysicalAddressConverter x:Key="interfacePhysicalAddressConverter"/>
        <local:InterfaceIPPropertiesConverter x:Key="interfaceIPPropertiesConverter"/>
        <local:IPv6GlobalStatisticsConverter x:Key="ipv6GlobalStatisticsConverter"/>
        <local:IPv4GlobalStatisticsConverter x:Key="ipv4GlobalStatisticsConverter"/>
        <local:ActiveTcpConnectionsConverter x:Key="activeTcpConnectionsConverter"/>
        <local:ActiveTcpListenerConverter x:Key="activeTcpListenerConverter"/>
        <local:ActiveUdpListenerConverter x:Key="activeUdpListenerConverter"/>
        <local:IPAddressInformationCollectionConverter x:Key="ipAddressInformationCollectionConverter"/>
        <local:IPAddressCollectionConverter x:Key="ipAddressCollectionConverter"/>
        <local:GatewayIPAddressInformationCollectionConverter x:Key="gatewayIPAddressInformationCollectionConverter"/>
        <local:MulticastIPAddressInformationCollectionConverter x:Key="multicastIPAddressInformationCollectionConverter"/>
        <local:UnicastIPAddressInformationCollectionConverter x:Key="unicastIPAddressInformationCollectionConverter"/>
        <local:RowPresenterSourceConverter x:Key="rowPresenterSourceConverter"/>
        <GridViewColumnCollection x:Key="gridViewColumnCollection">
            <GridViewColumn Header="プロパティ" DisplayMemberBinding="{Binding Path=Label}"/>
            <GridViewColumn Header="値"         DisplayMemberBinding="{Binding Path=Value}"/>
            <GridViewColumn Header="型"         DisplayMemberBinding="{Binding Path=Type}"/>
        </GridViewColumnCollection>
        <Style TargetType="Expander">
            <Setter Property="Margin" Value="5"/>
            <Setter Property="BorderBrush" Value="Gray"/>
            <Setter Property="BorderThickness" Value="1"/>
        </Style>
        <ControlTemplate TargetType="{x:Type HeaderedContentControl}" x:Key="ipAddressRowControlTemplate">
            <Expander Header="{TemplateBinding Header}">
                <StackPanel DataContext="{TemplateBinding Content}">
                    <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IPAddress}"/>
                    <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=AddressFamily, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=AddressFamily}"/>
                    <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsIPv6LinkLocal, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsIPv6LinkLocal}"/>
                    <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsIPv6Multicast, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsIPv6Multicast}"/>
                    <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsIPv6SiteLocal, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsIPv6SiteLocal}"/>
                    <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=ScopeId, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=ScopeId}"/>
                </StackPanel>
            </Expander>
        </ControlTemplate>
        <DataTemplate x:Key="ipAddreessRowTemplate">
            <HeaderedContentControl Template="{StaticResource ipAddressRowControlTemplate}" Header="{Binding}" Content="{Binding}"/>
        </DataTemplate>
        <ControlTemplate TargetType="{x:Type HeaderedContentControl}" x:Key="ipEndPointControlTemplate">
            <Expander Header="{TemplateBinding Header}">
                <StackPanel DataContext="{Binding}">
                    <ContentControl Content="{Binding Path=Address}" ContentTemplate="{StaticResource ipAddreessRowTemplate}"/>
                    <GridViewRowPresenter Content="{Binding Path=AddressFamily, ConverterParameter=AddressFamily, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                    <GridViewRowPresenter Content="{Binding Path=Port, ConverterParameter=Port, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                </StackPanel>
            </Expander>
        </ControlTemplate>
        <DataTemplate x:Key="ipEndPointTemplate">
            <HeaderedContentControl Template="{StaticResource ipEndPointControlTemplate}" Header="{Binding}" Content="{Binding}"/>
        </DataTemplate>
        <DataTemplate x:Key="ipGlobalStatisticsPanelTemplate">
            <StackPanel>
                <GridViewRowPresenter Content="{Binding Path=DefaultTtl, ConverterParameter=DefaultTtl, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=ForwardingEnabled, ConverterParameter=ForwardingEnabled, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=NumberOfInterfaces, ConverterParameter=NumberOfInterfaces, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=NumberOfIPAddresses, ConverterParameter=NumberOfIPAddresses, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=NumberOfRoutes, ConverterParameter=NumberOfRoutes, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=OutputPacketRequests, ConverterParameter=OutputPacketRequests, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=OutputPacketRoutingDiscards, ConverterParameter=OutputPacketRoutingDiscards, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=OutputPacketsDiscarded, ConverterParameter=OutputPacketsDiscarded, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=OutputPacketsWithNoRoute, ConverterParameter=OutputPacketsWithNoRoute, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=PacketFragmentFailures, ConverterParameter=PacketFragmentFailures, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=PacketReassembliesRequired, ConverterParameter=PacketReassembliesRequired, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=PacketReassemblyFailures, ConverterParameter=PacketReassemblyFailures, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=PacketReassemblyTimeout, ConverterParameter=PacketReassemblyTimeout, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=PacketsFragmented, ConverterParameter=PacketsFragmented, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=PacketsReassembled, ConverterParameter=PacketsReassembled, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=ReceivedPackets, ConverterParameter=ReceivedPackets, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=ReceivedPacketsDelivered, ConverterParameter=ReceivedPacketsDelivered, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=ReceivedPacketsDiscarded, ConverterParameter=ReceivedPacketsDiscarded, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=ReceivedPacketsForwarded, ConverterParameter=ReceivedPacketsForwarded, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=ReceivedPacketsWithAddressErrors, ConverterParameter=ReceivedPacketsWithAddressErrors, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=ReceivedPacketsWithHeadersErrors, ConverterParameter=ReceivedPacketsWithHeadersErrors, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                <GridViewRowPresenter Content="{Binding Path=ReceivedPacketsWithUnknownProtocol, ConverterParameter=ReceivedPacketsWithUnknownProtocol, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
            </StackPanel>

        </DataTemplate>
    </Window.Resources>
    <DockPanel>
        <GridViewHeaderRowPresenter DockPanel.Dock="Top" Columns="{StaticResource gridViewColumnCollection}"/>
        <ScrollViewer
        HorizontalScrollBarVisibility="Auto"
        VerticalScrollBarVisibility="Auto">
            <StackPanel DataContext="{StaticResource networkInfo}" Grid.IsSharedSizeScope="True">
                <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=HostName, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=HostName}"/>
                <Expander Header="AddressList">
                    <ItemsControl ItemsSource="{Binding Path=Address}" ItemTemplate="{StaticResource ipAddreessRowTemplate}" Focusable="False"/>
                </Expander>
                <Expander Header="NetworkInterfaces">
                    <ItemsControl ItemsSource="{Binding Path=NetworkInterfaces}" Focusable="False">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Expander Header="{Binding Path=Name}">
                                    <StackPanel>
                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=Name, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=Name}"/>
                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=Description, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=Description}"/>
                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=Id, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=Id}"/>
                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=NetworkInterfaceType, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=NetworkInterfaceType}"/>
                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsReceiveOnly, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsReceiveOnly}"/>
                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=OperationalStatus, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=OperationalStatus}"/>
                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=Speed, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=Speed}"/>
                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=SupportsMulticast, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=SupportsMulticast}"/>
                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" DataContext="{Binding Converter={StaticResource interfacePhysicalAddressConverter}}" Content="{Binding Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=PhysicalAddress}"/>
                                        <Expander Header="IPInterfaceProperties">
                                            <StackPanel DataContext="{Binding Converter={StaticResource interfaceIPPropertiesConverter}}">
                                                <Expander Header="AnycastAddresses">
                                                    <ItemsControl ItemsSource="{Binding Path=AnycastAddresses, Converter={StaticResource ipAddressInformationCollectionConverter}}">
                                                        <ItemsControl.ItemTemplate>
                                                            <DataTemplate>
                                                                <StackPanel>
                                                                    <ContentControl ContentTemplate="{StaticResource ipAddreessRowTemplate}"/>
                                                                    <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsDnsEligible, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsDnsEligible}"/>
                                                                    <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsTransient, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsTransient}"/>
                                                                </StackPanel>
                                                            </DataTemplate>
                                                        </ItemsControl.ItemTemplate>
                                                    </ItemsControl>
                                                </Expander>
                                                <Expander Header="DhcpServerAddresses">
                                                    <ItemsControl ItemsSource="{Binding Path=DhcpServerAddresses, Converter={StaticResource ipAddressCollectionConverter}}" ItemTemplate="{StaticResource ipAddreessRowTemplate}"/>
                                                </Expander>
                                                <Expander Header="DnsAddresses">
                                                    <ItemsControl ItemsSource="{Binding Path=DnsAddresses, Converter={StaticResource ipAddressCollectionConverter}}" ItemTemplate="{StaticResource ipAddreessRowTemplate}"/>
                                                </Expander>
                                                <GridViewRowPresenter Content="{Binding Path=DnsSuffix, ConverterParameter=DnsSuffix, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>

                                                <Expander Header="GatewayAddresses">
                                                    <ItemsControl ItemsSource="{Binding Path=GatewayAddresses, Converter={StaticResource gatewayIPAddressInformationCollectionConverter}}">
                                                        <ItemsControl.ItemTemplate>
                                                            <DataTemplate>
                                                                <HeaderedContentControl Header="{Binding Address}" Content="{Binding Address}" Template="{StaticResource ipAddressRowControlTemplate}"/>
                                                            </DataTemplate>
                                                        </ItemsControl.ItemTemplate>
                                                    </ItemsControl>
                                                </Expander>
                                                
                                                <GridViewRowPresenter Content="{Binding Path=IsDnsEnabled, ConverterParameter=IsDnsEnabled, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                                                <GridViewRowPresenter Content="{Binding Path=IsDynamicDnsEnabled, ConverterParameter=IsDynamicDnsEnabled, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>

                                                <Expander Header="MulticastAddresses" DataContext="{Binding Path=MulticastAddresses}">
                                                    <StackPanel>

                                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=Count, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=Count}"/>
                                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsReadOnly, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsReadOnly}"/>
                                                        <ItemsControl ItemsSource="{Binding Converter={StaticResource multicastIPAddressInformationCollectionConverter}}">
                                                            <ItemsControl.ItemTemplate>
                                                                <DataTemplate>
                                                                    <Expander Header="{Binding Path=Address}">
                                                                        <StackPanel>
                                                                            <HeaderedContentControl Header="Address" Content="{Binding Path=Address}" Template="{StaticResource ipAddressRowControlTemplate}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=AddressPreferredLifetime, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=AddressPreferredLifetime}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=AddressValidLifetime, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=AddressValidLifetime}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=DhcpLeaseLifetime, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=DhcpLeaseLifetime}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=DuplicateAddressDetectionState, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=DuplicateAddressDetectionState}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsDnsEligible, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsDnsEligible}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsTransient, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsTransient}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=PrefixOrigin, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=PrefixOrigin}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=SuffixOrigin, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=SuffixOrigin}"/>
                                                                        </StackPanel>
                                                                    </Expander>
                                                                </DataTemplate>
                                                            </ItemsControl.ItemTemplate>
                                                        </ItemsControl>
                                                    </StackPanel>
                                                </Expander>
                                                <Expander Header="UnicastAddresses">
                                                    <StackPanel DataContext="{Binding Path=UnicastAddresses}">

                                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=Count, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=Count}"/>
                                                        <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsReadOnly, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsReadOnly}"/>
                                                        <ItemsControl ItemsSource="{Binding Converter={StaticResource unicastIPAddressInformationCollectionConverter}}">
                                                            <ItemsControl.ItemTemplate>
                                                                <DataTemplate>
                                                                    <Expander Header="{Binding Path=Address}">
                                                                        <StackPanel>
                                                                            <HeaderedContentControl Header="Address" Content="{Binding Path=Address}" Template="{StaticResource ipAddressRowControlTemplate}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=AddressPreferredLifetime, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=AddressPreferredLifetime}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=AddressValidLifetime, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=AddressValidLifetime}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=DhcpLeaseLifetime, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=DhcpLeaseLifetime}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=DuplicateAddressDetectionState, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=DuplicateAddressDetectionState}"/>
                                                                            <HeaderedContentControl Header="IPv4Mask" Content="{Binding Path=IPv4Mask}" Template="{StaticResource ipAddressRowControlTemplate}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsDnsEligible, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsDnsEligible}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=IsTransient, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=IsTransient}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=PrefixOrigin, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=PrefixOrigin}"/>
                                                                            <GridViewRowPresenter Columns="{StaticResource gridViewColumnCollection}" Content="{Binding Path=SuffixOrigin, Converter={StaticResource rowPresenterSourceConverter}, ConverterParameter=SuffixOrigin}"/>
                                                                        </StackPanel>
                                                                    </Expander>
                                                                </DataTemplate>
                                                            </ItemsControl.ItemTemplate>
                                                        </ItemsControl>
                                                    </StackPanel>
                                                </Expander>
                                                <Expander Header="WinsServersAddresses">
                                                    <ItemsControl ItemsSource="{Binding Path=WinsServersAddresses, Converter={StaticResource ipAddressCollectionConverter}}" ItemTemplate="{StaticResource ipAddreessRowTemplate}"/>
                                                </Expander>

                                            </StackPanel>
                                        </Expander>
                                    </StackPanel>
                                </Expander>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Expander>
                <Expander Header="IPGlobalProperties">
                    <StackPanel DataContext="{Binding Path=IPGlobalProperties}">
                        <GridViewRowPresenter Content="{Binding Path=DhcpScopeName, ConverterParameter=DhcpScopeName, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                        <GridViewRowPresenter Content="{Binding Path=DomainName, ConverterParameter=DomainName, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                        <GridViewRowPresenter Content="{Binding Path=HostName, ConverterParameter=HostName, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                        <GridViewRowPresenter Content="{Binding Path=IsWinsProxy, ConverterParameter=IsWinsProxy, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                        <GridViewRowPresenter Content="{Binding Path=NodeType, ConverterParameter=NodeType, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                        <Expander Header="ActiveTcpListeners">
                            <ItemsControl ItemsSource="{Binding Converter={StaticResource activeTcpListenerConverter}}" ItemTemplate="{StaticResource ipEndPointTemplate}"/>
                        </Expander>
                        <Expander Header="ActiveUdpListeners">
                            <ItemsControl ItemsSource="{Binding Converter={StaticResource activeUdpListenerConverter}}" ItemTemplate="{StaticResource ipEndPointTemplate}"/>
                        </Expander>
                        <Expander Header="ActiveTcpConnections">
                            <StackPanel DataContext="{Binding Converter={StaticResource activeTcpConnectionsConverter}}">
                                <HeaderedContentControl DataContext="{Binding Path=LocalEndPoint}" Header="LocalEndPoint" Template="{StaticResource ipEndPointControlTemplate}"/>
                                <HeaderedContentControl DataContext="{Binding Path=RemoteEndPoint}" Header="RemoteEndPoint" Template="{StaticResource ipEndPointControlTemplate}"/>
                                <GridViewRowPresenter Content="{Binding Path=State, ConverterParameter=State, Converter={StaticResource rowPresenterSourceConverter}}" Columns="{StaticResource gridViewColumnCollection}"/>
                            </StackPanel>
                        </Expander>
                        <Expander Header="IPv6GlobalStatistics" Content="{Binding Converter={StaticResource ipv6GlobalStatisticsConverter}}" ContentTemplate="{StaticResource ipGlobalStatisticsPanelTemplate}"/>
                        <Expander Header="IPv4GlobalStatistics" Content="{Binding Converter={StaticResource ipv4GlobalStatisticsConverter}}" ContentTemplate="{StaticResource ipGlobalStatisticsPanelTemplate}"/>
                    </StackPanel>
                </Expander>
            </StackPanel>
        </ScrollViewer>
    </DockPanel>
</Window>
// Window1.xaml.cs
using System;
using System.Windows;
using System.Windows.Data;

namespace NetworkInfomationSample
{
    /// <summary>
    /// Window1.xaml の相互作用ロジック
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1 ()
        {
            InitializeComponent ();
        }
    }

    /// <summary>
    /// ネットワーク情報を取得するためのクラス
    /// </summary>
    public class NetworkInfo
    {
        string hostName;
        System.Net.IPHostEntry ipHostEntry;
        System.Net.NetworkInformation.NetworkInterface [] networkInterface;
        System.Net.NetworkInformation.IPGlobalProperties ipGlobalProperties;

        /// <summary>
        /// コンストラクタ
        /// </summary>
        public NetworkInfo ()
        {
            hostName = System.Net.Dns.GetHostName ();
            ipHostEntry = System.Net.Dns.GetHostEntry (hostName);
            networkInterface = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces ();
            ipGlobalProperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties ();
        }

        public string HostName { get { return hostName; } }
        public string Entry { get { return ipHostEntry.ToString (); } }
        public System.Net.IPAddress [] Address { get { return ipHostEntry.AddressList; } }
        public string [] Aliases { get { return ipHostEntry.Aliases; } }

        public System.Net.NetworkInformation.NetworkInterface [] NetworkInterfaces { get { return networkInterface; } }
        public System.Net.NetworkInformation.IPGlobalProperties IPGlobalProperties { get { return ipGlobalProperties; } } 
    }
    
    /// <summary>
    /// 行にバインドするためのオブジェクト
    /// </summary>
    public class PropertyData
    {
        public string Label { get; set; }
        public string Value { get; set; }
        public string Type { get; set; }
    }

    /// <summary>
    /// 取得した情報から行を生成する値コンバータ
    /// </summary>
    #region RowPresenterSourceConverter
    [ValueConversion (typeof (object), typeof (PropertyData))]
    public class RowPresenterSourceConverter : IValueConverter
    {
        /// <summary>
        /// 値から PropertyData オブジェクトを生成して返す
        /// </summary>
        /// <param name="value">値ソース</param>
        /// <param name="targetType">ターゲットプロパティの型</param>
        /// <param name="parameter">情報のラベル</param>
        /// <param name="culture">使用するカルチャ</param>
        /// <returns>PropertyData 型のオブジェクト</returns>
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var label = parameter as string;
            var val = "(null)";
            var type = "Unknown";
            if (value != null)
            {
                val = value.ToString ();
                type = value.GetType ().ToString ();
            }

            return new PropertyData { Label = label, Value = val, Type = type };
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }
    #endregion

    /// <summary>
    /// NIC の MAC アドレスを取得する値コンバータ
    /// </summary>
    #region InterfacePhysicalAddressConverter
    [ValueConversion (typeof (System.Net.NetworkInformation.NetworkInterface), typeof (System.Net.NetworkInformation.PhysicalAddress))]
    public class InterfacePhysicalAddressConverter : IValueConverter
    {
        /// <summary>
        /// NetworkInterface の MAC アドレスを取得して返す
        /// </summary>
        /// <param name="value">値ソース(NetworkInterface 型)</param>
        /// <param name="targetType">ターゲットプロパティの型</param>
        /// <param name="parameter">未使用</param>
        /// <param name="culture">変換に使用するカルチャ</param>
        /// <returns>PhysicalAddress オブジェクト</returns>
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.NetworkInterface;

            if (source == null)
            {
                return DependencyProperty.UnsetValue;
            }

            return source.GetPhysicalAddress ();
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }
    #endregion

    /// <summary>
    /// NetworkInterface オブジェクトの GetIPProperties () を実行した結果を返す値コンバータ
    /// </summary>
    #region InterfaceIPPropertiesConverter
    [ValueConversion (typeof (System.Net.NetworkInformation.NetworkInterface), typeof (System.Net.NetworkInformation.IPInterfaceProperties))]
    public class InterfaceIPPropertiesConverter : IValueConverter
    {
        /// <summary>
        /// NetworkInterface オブジェクトの GetIPProperties () を実行した結果を返す
        /// </summary>
        /// <param name="value">値ソース(NetworkInterface 型)</param>
        /// <param name="targetType">ターゲットプロパティの型</param>
        /// <param name="parameter">未使用</param>
        /// <param name="culture">変換に使用するカルチャ</param>
        /// <returns>IPInterfaceProperty 型</returns>
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.NetworkInterface;

            if (source == null)
            {
                return DependencyProperty.UnsetValue;
            }

            return source.GetIPProperties ();
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }
    #endregion

    /// <summary>
    /// IPGlobalProperties オブジェクトの GetIPv6GlobalStatistics () を実行した結果を返す値コンバータ
    /// </summary>
    [ValueConversion (typeof (System.Net.NetworkInformation.IPGlobalProperties), typeof (System.Net.NetworkInformation.IPGlobalStatistics))]
    public class IPv6GlobalStatisticsConverter : IValueConverter
    {
        /// <summary>
        /// IPGlobalProperties オブジェクトの GetIPv6GlobalStatistics () を実行した結果を返す
        /// </summary>
        /// <param name="value">値ソース(IPGlobalProperties 型)</param>
        /// <param name="targetType">ターゲットプロパティの型</param>
        /// <param name="parameter">未使用</param>
        /// <param name="culture">変換に使用するカルチャ</param>
        /// <returns>IPGlobalStatistics 型</returns>
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.IPGlobalProperties;

            if (source == null)
            {
                return DependencyProperty.UnsetValue;
            }

            return source.GetIPv6GlobalStatistics ();
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }

    /// <summary>
    /// IPGlobalProperties オブジェクトの GetIPv4GlobalStatistics () を実行した結果を返す値コンバータ
    /// </summary>
    [ValueConversion (typeof (System.Net.NetworkInformation.IPGlobalProperties), typeof (System.Net.NetworkInformation.IPGlobalStatistics))]
    public class IPv4GlobalStatisticsConverter : IValueConverter
    {
        /// <summary>
        /// IPGlobalProperties オブジェクトの GetIPv4GlobalStatistics () を実行した結果を返す
        /// </summary>
        /// <param name="value">値ソース(IPGlobalProperties 型)</param>
        /// <param name="targetType">ターゲットプロパティの型</param>
        /// <param name="parameter">未使用</param>
        /// <param name="culture">変換に使用するカルチャ</param>
        /// <returns>IPGlobalStatistics 型</returns>
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.IPGlobalProperties;

            if (source == null)
            {
                return DependencyProperty.UnsetValue;
            }

            return source.GetIPv4GlobalStatistics ();
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = (System.Net.NetworkInformation.IPGlobalStatistics) value;

            //TODO:System.Net.NetworkInformation.IPGlobalStatistics から System.Net.NetworkInformation.IPGlobalProperties への変換を記述します。
            throw new NotImplementedException ();
        }
    }

    /// <summary>
    /// IPGlobalProperties オブジェクトの GetActiveTcpListeners () を実行した結果を返す値コンバータ
    /// </summary>
    [ValueConversion (typeof (System.Net.NetworkInformation.IPGlobalProperties), typeof (System.Net.IPEndPoint []))]
    public class ActiveTcpListenerConverter : IValueConverter
    {
        /// <summary>
        /// IPGlobalProperties オブジェクトの GetActiveTcpListeners () を実行した結果を返す
        /// </summary>
        /// <param name="value">値ソース(IPGlobalProperties 型)</param>
        /// <param name="targetType">ターゲットプロパティの型</param>
        /// <param name="parameter">未使用</param>
        /// <param name="culture">変換に使用するカルチャ</param>
        /// <returns>IPEndPoint 型の配列</returns>
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.IPGlobalProperties;

            if (source == null)
            {
                return DependencyProperty.UnsetValue;
            }

            return source.GetActiveTcpListeners ();
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }
    [ValueConversion (typeof (System.Net.NetworkInformation.IPGlobalProperties), typeof (System.Net.IPEndPoint []))]
    public class ActiveUdpListenerConverter : IValueConverter
    {
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.IPGlobalProperties;
            return source.GetActiveUdpListeners ();
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }
    [ValueConversion (typeof (System.Net.NetworkInformation.IPGlobalProperties), typeof (System.Net.NetworkInformation.TcpConnectionInformation []))]
    public class ActiveTcpConnectionsConverter : IValueConverter
    {
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.IPGlobalProperties;
            return source.GetActiveTcpConnections ();
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }



    [ValueConversion (typeof (System.Net.NetworkInformation.IPAddressInformationCollection), typeof (System.Collections.IEnumerable))]
    public class IPAddressInformationCollectionConverter : IValueConverter
    {
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.IPAddressInformationCollection;

            if (source == null)
            {
                return null;
            }
            var collection = new System.Collections.ObjectModel.ObservableCollection<System.Net.NetworkInformation.IPAddressInformation> ();
            foreach (var addr in source)
            {
                collection.Add (addr);
            }
            return collection;
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }


    [ValueConversion (typeof (System.Net.NetworkInformation.IPAddressCollection), typeof (System.Collections.IEnumerable))]
    public class IPAddressCollectionConverter : IValueConverter
    {
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.IPAddressCollection;
            if (source == null)
            {
                return null;
            }
            var collection = new System.Collections.ObjectModel.ObservableCollection<System.Net.IPAddress> ();
            foreach (var addr in source)
            {
                collection.Add (addr);
            }
            return collection;

        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }

    [ValueConversion (typeof (System.Net.NetworkInformation.GatewayIPAddressInformationCollection), typeof (System.Collections.IEnumerable))]
    public class GatewayIPAddressInformationCollectionConverter : IValueConverter
    {
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.GatewayIPAddressInformationCollection;
            if (source == null)
            {
                return null;
            }
            var collection = new System.Collections.ObjectModel.ObservableCollection<System.Net.NetworkInformation.GatewayIPAddressInformation> ();
            foreach (var addr in source)
            {
                collection.Add (addr);
            }
            return collection;
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }

    [ValueConversion (typeof (System.Net.NetworkInformation.MulticastIPAddressInformationCollection), typeof (System.Collections.IEnumerable))]
    public class MulticastIPAddressInformationCollectionConverter : IValueConverter
    {
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.MulticastIPAddressInformationCollection;
            if (source == null)
            {
                return null;
            }
            var collection = new System.Collections.ObjectModel.ObservableCollection<System.Net.NetworkInformation.MulticastIPAddressInformation> ();
            foreach (var addr in source)
            {
                collection.Add (addr);
            }
            return collection;
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }

    [ValueConversion (typeof (System.Net.NetworkInformation.UnicastIPAddressInformationCollection), typeof (System.Collections.IEnumerable))]
    public class UnicastIPAddressInformationCollectionConverter : IValueConverter
    {
        public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var source = value as System.Net.NetworkInformation.UnicastIPAddressInformationCollection;
            if (source == null)
            {
                return null;
            }
            var collection = new System.Collections.ObjectModel.ObservableCollection<System.Net.NetworkInformation.UnicastIPAddressInformation> ();
            foreach (var addr in source)
            {
                collection.Add (addr);
            }
            return collection;
        }

        public object ConvertBack (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException ();
        }
    }
}

トラックバック

このエントリーにトラックバック:
http://frog.raindrop.jp/cgi-bin/mt/mt-tb.cgi/2219

コメント

コメントする

※ コメントスパム対策のため、コメント本文はおはよう、こんにちわ、こんばんわのいずれかより始めるようにしてください。

name:
email:

※ 必要ですが、表示しません。

url:
情報を保存する ?