Vb.net 默认情况下使用PC名称
我完成了局域网聊天项目,一切都很好, 但是我这里有个问题,我不希望用户在每次通过局域网发送MSG时都输入自己的名字,我希望程序默认使用PC名,所以在聊天区PC名会自动出现,有人能帮我吗 &这是我的密码Vb.net 默认情况下使用PC名称,vb.net,chat,Vb.net,Chat,我完成了局域网聊天项目,一切都很好, 但是我这里有个问题,我不希望用户在每次通过局域网发送MSG时都输入自己的名字,我希望程序默认使用PC名,所以在聊天区PC名会自动出现,有人能帮我吗 &这是我的密码 Imports System.DirectoryServices Imports System.Net Imports System.IO Imports System.Net.Sockets Imports MSTSCLib Public Class frmMain Inherits
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
导入System.DirectoryServices
导入系统.Net
导入System.IO
导入System.Net.Sockets
导入MSTSCLib
公开课
继承System.Windows.Forms.Form
#区域“Windows窗体设计器生成的代码”
公共分新()
MyBase.New()
'此调用是Windows窗体设计器所必需的。
初始化组件()
'在InitializeComponent()调用之后添加任何初始化
端接头
'窗体覆盖dispose以清理组件列表。
受保护的重载重写子处置(ByVal作为布尔值处置)
如果是这样的话
如果不是(组件什么都不是),那么
components.Dispose()
如果结束
如果结束
MyBase.Dispose(Dispose)
端接头
'是Windows窗体设计器所必需的
作为System.ComponentModel.IContainer的专用组件
'注意:Windows窗体设计器需要以下过程
'可以使用Windows窗体设计器对其进行修改。
'不要使用代码编辑器修改它。
Friend with Events TreeView 1作为System.Windows.Forms.TreeView
Friend WithEvents TXTPName As System.Windows.Forms.TextBox
Friend WithEvents txtSend As System.Windows.Forms.TextBox
Friend WithEvents CmdSend As System.Windows.Forms.Button
Friend WithEvents ImageList 1作为System.Windows.Forms.ImageList
Friend with events txtpcipad As System.Windows.Forms.TextBox
Friend with Events Label3作为System.Windows.Forms.Label
Friend with Events Timer1作为System.Windows.Forms.Timer
Friend WithEvents txtUsername为System.Windows.Forms.TextBox
Friend with Events Label4作为System.Windows.Forms.Label
Friend with Events Label5作为System.Windows.Forms.Label
Friend WithEvents txtConversation As System.Windows.Forms.TextBox
Friend WithEvents txttempmsg As System.Windows.Forms.TextBox
Friend with Events Label2作为System.Windows.Forms.Label
私有子初始化组件()
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=新系统.Windows.Forms.TreeView()
Me.ImageList1=新建System.Windows.Forms.ImageList(Me.components)
Me.txtpname=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=新系统.Windows.Forms.Timer(Me.components)
Me.txtUsername=New System.Windows.Forms.TextBox()
Me.Label4=新的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=新建System.Drawing.Font(“Microsoft Sans Serif”,9.0!,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,CType(0,字节))
Me.CmdSend.Location=新系统.Drawing.Point(404265)
Me.CmdSend.Name=“CmdSend”
Me.CmdSend.Size=新系统.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=新系统图纸点(291,10)
Me.TreeView1.Name=“TreeView1”
Me.TreeView1.SelectedImageIndex=0
Me.TreeView1.Size=新系统.图纸.尺寸(168,234)
Me.TreeView1.TabIndex=1
'
'图像列表1
'
Me.ImageList1.ImageStream=CType(resources.GetObject(“ImageList1.ImageStream”)、System.Windows.Forms.ImageListStreamer)
Me.ImageList1.TransparentColor=System.Drawing.Color.Transparent
Me.ImageList1.Images.SetKeyName(0,“”)
'
“txtpname
'
Me.txtpname.Location=新系统.图纸.点(108,34)
Me.txtpname.Name=“txtpname”
Me.txtpname.ReadOnly=True
Me.txtpname.Size=新系统.图纸.尺寸(136,20)
Me.txtpname.TabIndex=2
'
“txtssend
'
Me.txtSend.Location=新系统.图纸.点(12,266)
Me.txtSend.Multiline=True
Me.txtSend.Name=“txtSend”
Me.txtSend.ScrollBars=System.Windows.Forms.ScrollBars.Both
Me.txtSend.Size=新系统.图纸.尺寸(386,52)
Me.txtSend.TabIndex=3
'
“txtPCIPadd
'
Me.txtPCIPadd.Location=新系统.图纸.点(108,58)
Me.txtPCIPadd.Name=“txtPCIPadd”
Me.txtPCIPadd.Size=新系统.图纸.尺寸(136,20)
Me.txtPCIPadd.TabIndex=6
'
“标签2
'
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,字节))
Me.Label2.Location=新系统.图纸.点(13,59)
Me.Label2.Name=“Label2”
Me.Label2.Size=新系统.图纸.尺寸(71,15)
Me.Label2.TabIndex=7
Me.Label2.Text=“IP地址:”
'
“标签3
'
Me.Label3.AutoSize=True
Me.Label3.Font=New System.Drawing.Font(“Microsoft Sans Serif”,9.0!,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,CTy
System.Environment.MachineName
txtUsername.Text = System.Environment.MachineName
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
Imports System.Management