我完成了我的Chat over LAN项目,一切运行良好, 但我在这里遇到问题,我不希望用户每次想通过局域网发送消息时都会输入他们的名字,我希望程序默认使用PC名称,所以在聊天部分PC名称会自动出现,可以有人帮我这个!
&安培;这是我的代码
Imports System.DirectoryServices
Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Imports MSTSCLib
Public Class frmMain
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
Friend WithEvents txtPCName As System.Windows.Forms.TextBox
Friend WithEvents txtSend As System.Windows.Forms.TextBox
Friend WithEvents CmdSend As System.Windows.Forms.Button
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents txtPCIPadd As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Friend WithEvents txtUsername As System.Windows.Forms.TextBox
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents txtConversation As System.Windows.Forms.TextBox
Friend WithEvents txttempmsg As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmMain))
Me.CmdSend = New System.Windows.Forms.Button()
Me.TreeView1 = New System.Windows.Forms.TreeView()
Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
Me.txtPCName = New System.Windows.Forms.TextBox()
Me.txtSend = New System.Windows.Forms.TextBox()
Me.txtPCIPadd = New System.Windows.Forms.TextBox()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.txtUsername = New System.Windows.Forms.TextBox()
Me.Label4 = New System.Windows.Forms.Label()
Me.Label5 = New System.Windows.Forms.Label()
Me.txtConversation = New System.Windows.Forms.TextBox()
Me.txttempmsg = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'CmdSend
'
Me.CmdSend.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.CmdSend.Location = New System.Drawing.Point(404, 265)
Me.CmdSend.Name = "CmdSend"
Me.CmdSend.Size = New System.Drawing.Size(55, 52)
Me.CmdSend.TabIndex = 0
Me.CmdSend.Text = "&Send"
'
'TreeView1
'
Me.TreeView1.ImageIndex = 0
Me.TreeView1.ImageList = Me.ImageList1
Me.TreeView1.Location = New System.Drawing.Point(291, 10)
Me.TreeView1.Name = "TreeView1"
Me.TreeView1.SelectedImageIndex = 0
Me.TreeView1.Size = New System.Drawing.Size(168, 234)
Me.TreeView1.TabIndex = 1
'
'ImageList1
'
Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
Me.ImageList1.Images.SetKeyName(0, "")
'
'txtPCName
'
Me.txtPCName.Location = New System.Drawing.Point(108, 34)
Me.txtPCName.Name = "txtPCName"
Me.txtPCName.ReadOnly = True
Me.txtPCName.Size = New System.Drawing.Size(136, 20)
Me.txtPCName.TabIndex = 2
'
'txtSend
'
Me.txtSend.Location = New System.Drawing.Point(12, 266)
Me.txtSend.Multiline = True
Me.txtSend.Name = "txtSend"
Me.txtSend.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.txtSend.Size = New System.Drawing.Size(386, 52)
Me.txtSend.TabIndex = 3
'
'txtPCIPadd
'
Me.txtPCIPadd.Location = New System.Drawing.Point(108, 58)
Me.txtPCIPadd.Name = "txtPCIPadd"
Me.txtPCIPadd.Size = New System.Drawing.Size(136, 20)
Me.txtPCIPadd.TabIndex = 6
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label2.Location = New System.Drawing.Point(13, 59)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(71, 15)
Me.Label2.TabIndex = 7
Me.Label2.Text = "IP Address :"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(12, 247)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(58, 15)
Me.Label3.TabIndex = 9
Me.Label3.Text = "Message"
'
'Timer1
'
'
'txtUsername
'
Me.txtUsername.Location = New System.Drawing.Point(108, 10)
Me.txtUsername.Name = "txtUsername"
Me.txtUsername.Size = New System.Drawing.Size(136, 20)
Me.txtUsername.TabIndex = 10
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label4.Location = New System.Drawing.Point(13, 14)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(70, 15)
Me.Label4.TabIndex = 11
Me.Label4.Text = "Your name:"
'
'Label5
'
Me.Label5.AutoSize = True
Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label5.Location = New System.Drawing.Point(13, 35)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(44, 15)
Me.Label5.TabIndex = 12
Me.Label5.Text = "Buddy:"
'
'txtConversation
'
Me.txtConversation.BackColor = System.Drawing.Color.White
Me.txtConversation.Location = New System.Drawing.Point(15, 84)
Me.txtConversation.Multiline = True
Me.txtConversation.Name = "txtConversation"
Me.txtConversation.ReadOnly = True
Me.txtConversation.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.txtConversation.Size = New System.Drawing.Size(270, 160)
Me.txtConversation.TabIndex = 14
'
'txttempmsg
'
Me.txttempmsg.Enabled = False
Me.txttempmsg.Location = New System.Drawing.Point(12, 342)
Me.txttempmsg.Multiline = True
Me.txttempmsg.Name = "txttempmsg"
Me.txttempmsg.Size = New System.Drawing.Size(212, 23)
Me.txttempmsg.TabIndex = 15
Me.txttempmsg.Visible = False
'
'frmMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.SystemColors.ButtonFace
Me.ClientSize = New System.Drawing.Size(471, 329)
Me.Controls.Add(Me.txttempmsg)
Me.Controls.Add(Me.txtConversation)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.txtUsername)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.txtPCIPadd)
Me.Controls.Add(Me.txtSend)
Me.Controls.Add(Me.txtPCName)
Me.Controls.Add(Me.TreeView1)
Me.Controls.Add(Me.CmdSend)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "frmMain"
Me.Text = "KiWi Messenger"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
#End Region
Dim listerner As New TcpListener(44444)
Dim client As TcpClient
Dim client2 As TcpClient
Dim message As String = ""
Dim tts As Object
Dim Sound As New System.Media.SoundPlayer()
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
listerner.Stop()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TextBox1.Selectionstart = TextBox1.TextLength
'TextBox1.ScrollToCaret()
Dim childEntry As DirectoryEntry
Dim ParentEntry As New DirectoryEntry()
Try
ParentEntry.Path = "WinNT:"
For Each childEntry In ParentEntry.Children
Dim newNode As New TreeNode(childEntry.Name)
Select Case childEntry.SchemaClassName
Case "Domain"
Dim ParentDomain As New TreeNode(childEntry.Name)
TreeView1.Nodes.AddRange(New TreeNode() {ParentDomain})
Dim SubChildEntry As DirectoryEntry
Dim SubParentEntry As New DirectoryEntry()
SubParentEntry.Path = "WinNT://" & childEntry.Name
For Each SubChildEntry In SubParentEntry.Children
Dim newNode1 As New TreeNode(SubChildEntry.Name)
Select Case SubChildEntry.SchemaClassName
Case "Computer"
ParentDomain.Nodes.Add(newNode1)
End Select
Next
End Select
Next
Catch Excep As Exception
MsgBox("Error While Reading Directories")
Finally
ParentEntry = Nothing
End Try
listerner.Start()
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSend.Click
' Shell("net send " & txtcomputer.Text & " " & txtmessage.Text)
Try
If txtPCIPadd.Text = "" Or txtUsername.Text = "" Or txtSend.Text = "" Then
MsgBox("Sorry Incomplete data")
Else
client = New TcpClient(txtPCIPadd.Text, 44444)
Dim writer As New StreamWriter(client.GetStream())
txttempmsg.Text = (txtSend.Text)
writer.Write(txtUsername.Text + " says: " + txtSend.Text)
txtConversation.Text = (txtConversation.Text + txtUsername.Text + " says: " + txttempmsg.Text + vbCrLf)
'txtmsg.Text="You:" + txtmessage.Text)
writer.Flush()
txtSend.Text = ""
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
txtPCName.Text = TreeView1.SelectedNode.Text
txtPCIPadd.Text = GetIPAddress(txtPCName.Text)
End Sub
Function GetIPAddress(ByVal CompName As String) As String
Dim oAddr As System.Net.IPAddress
Dim sAddr As String
Try
With System.Net.Dns.GetHostByName(CompName)
oAddr = New System.Net.IPAddress(.AddressList(0).Address)
sAddr = oAddr.ToString
End With
GetIPAddress = sAddr
Catch Excep As Exception
MsgBox(Excep.Message, MsgBoxStyle.OkOnly, "Lan Messenger")
Finally
End Try
End Function
Private Sub CmdPing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Shell("PING " & txtPCIPadd.Text)
End Sub
'Shell("net send ALL " & txtmessage.Text)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
If listerner.Pending = True Then
message = ""
client = listerner.AcceptTcpClient
Dim reader As New StreamReader(client.GetStream())
While reader.Peek > -1
message = message + Convert.ToChar(reader.Read()).ToString
End While
Me.Focus()
txtConversation.Text = (txtConversation.Text + message + vbCrLf)
txtConversation.SelectionStart = txtConversation.TextLength
txtConversation.ScrollToCaret()
'txtmsg.Text="You:" + txtmessage.Text)
My.Computer.Audio.Play(My.Resources.alert, AudioPlayMode.Background)
Sound.Load()
Sound.Play()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Function c() As String
Throw New NotImplementedException
End Function
Private Sub txtUsername_TextChanged(sender As Object, e As EventArgs) Handles txtUsername.TextChanged
End Sub
End Class
答案 0 :(得分:1)
您可以使用
检索计算机的名称System.Environment.MachineName
...您可以将其分配给变量,在您的实例中将是:
txtUsername.Text = System.Environment.MachineName
或者,如果您喜欢以冗长的方式做事,可以使用WMI(Windows Management Instrumentation),如下所示:
Dim details() As String = {"Name"}
Dim wmiItems As Dictionary(Of String, String)
wmiItems = WMI.GetWMISettingsDictionary("Win32_ComputerSystem", details)
For Each kvp As KeyValuePair(Of String, String) In wmiItems
txtUsername.Text = kvp.Value
Next
您也可以以相同的方式获取其他系统信息,例如品牌和型号; refer to the documentation为此。
要使用WMI,您必须参考System.Management
库,将以下内容放在代码文件的顶部:
Imports System.Management