获取当前使用我的winforms应用程序的计算机

时间:2015-09-15 05:22:49

标签: c# .net vb.net winforms

有人可以就如何实现这个目标给我一些想法吗?

我想要一个监控应用程序,列出当前打开我的winform应用程序的计算机的计算机名称和IP地址,当然计算机名称和ip应该在关闭应用程序时消失。

我想过只使用我在应用程序启动或关闭时更新的数据库表,但如果应用程序异常终止且表未更新,这会给我带来问题。

我正在看这里的实时监控,如果我打开监控应用程序然后我让所有PC运行应用程序,那么列表将以X间隔更新。

感谢。

1 个答案:

答案 0 :(得分:0)

  1. 您可以使用DispatcherTimer以时间间隔更新状态。
  2. 使用以下代码检测IP地址

    private static string GetIpAddress()
    {
        string localIP = "?";
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork))
        {
            localIP = ip.ToString();
        }
        return localIP;
    }
    
  3. 获取计算机名称

    var name = Environment.MachineName;
    
  4. 要检测系统将在应用程序运行后台时关闭并需要更新状态

    private bool _systemShutdown;
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WmQueryendSession)
        {
            _systemShutdown = true; // call here method to update status
        }
        base.WndProc(ref m);
    }
    
  5. 检测LAN状态,Unplugged或connected

    NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
    
    using Microsoft.Win32;
    
    private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
    {            
        _connectionStatus = !e.IsAvailable ? ConnectionStatus.Unplugged : ConnectionStatus.Connected;
    }
    
  6. 获取上次机器登录时间

    private static DateTime? GetLastLoginToMachine(string machineName, string userName)
    {
        try
        {
            var c = new PrincipalContext(ContextType.Machine, machineName);
            using (var uc = UserPrincipal.FindByIdentity(c, userName))
            {
                if (uc != null) return uc.LastLogon;
            }
        }
        catch (Exception ex)
        {
            ExceptionLogger.CreateFile(ex);
        }
        return null;
    }
    
  7. 将应用程序设置为“启动时运行”

    private void RunWindowsStartup(object sender, EventArgs e)
    {
        var menuItem = ((MenuItem)(sender));
        menuItem.Checked = !menuItem.Checked;
        RegisterInStartup(menuItem.Checked);
    }
    private static void RegisterInStartup(bool isChecked)
    {
        try
        {
            var appName = AppDomain.CurrentDomain.FriendlyName.Split('.')[0];
            using (var registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
            {
                if (registryKey == null) return;
                if (isChecked)
                {
                    registryKey.SetValue(appName, Application.ExecutablePath);
                }
                else
                {
                    registryKey.DeleteValue(appName);
                }
            }
        }
        catch (Exception ex)
        {
            ExceptionLogger.CreateFile(ex);
            TlMessageBox.Show(ex.Message, "Error", TlMessageBoxButtons.OK, TlMessageBoxIcon.Error, ex.StackTrace);
        }
    }
    
  8. 获得活跃的InActive(空闲)时间

    [DllImport("User32.dll")]
    public static extern bool LockWorkStation();
    [DllImport("User32.dll")]
    private static extern bool GetLastInputInfo(ref LASTINPUTINFO Dummy);
    public static uint GetIdleTime()
    {
        var LastUserAction = new LASTINPUTINFO();
        LastUserAction.cbSize = (uint)Marshal.SizeOf(LastUserAction);
        GetLastInputInfo(ref LastUserAction);
        return ((uint)Environment.TickCount - LastUserAction.dwTime);
    }
    internal struct LASTINPUTINFO
    {
        public uint cbSize;
        public uint dwTime;
    }
    
    // Use System.Windows.Form.Timer Tick event for further calculations ...