1

我正在尝试在 VB.Net 中创建一个“聊天”应用程序,并且 - 对于编程来说相当新 - 我正在努力解决两个阻止我的程序运行的问题。

该程序的主要目的是创建一个名为 Chat_Server.txt 的文件,用户可以创建/打开该文件。然后将此文件中的文本写入第一个文本框(称为 server_text)。在任何时候,用户还可以在第二个文本框(称为 client_text)中输入文本并按下“发送”按钮,然后将框的内容写入 Chat_Server.txt。

该程序的主要目标是两个或多个副本可以在不同的计算机上运行,​​并通过共享网络进行通信,而无需访问外部服务器。我的第一个问题是程序无法打开文件。Visual Studio 说该文件正在被另一个进程使用。

我的第二个问题是,只要不同计算机上的其他用户按下发送按钮,server_text 文本框中的文本就不会更新。我试图通过使用计时器来克服这个问题,即使由于某种原因这也不起作用。我也试过 fileSystemWatcher 也不管用。

这是我的代码(请记住,这是一个表单,而不是控制台):

Imports System.IO

Public Class Chat

Dim file_name As String

Sub Chat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Sub Create_Server_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Create_Server.Click 'This is a button on the form that get the user to choose a location to save the Chat_Server.txt

    SaveFileDialog1.ShowDialog()    'Save the file
    SaveFileDialog1.OpenFile()
    file_name = SaveFileDialog1.FileName()  'Get the file path
    MsgBox("Server file made.")

End Sub

Sub Connect_Server_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect_Server.Click   'Another button on the form that gets the user to choose the Chat_Server.txt

    OpenFileDialog1.ShowDialog()    'Open the file
    file_name = OpenFileDialog1.FileName()
    Dim fileExists As Boolean   'Tests whether the file existx and whether it works or not
    fileExists = My.Computer.FileSystem.FileExists(file_name)
    If fileExists = True Then
        Try
            Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)
        Catch ex As Exception
            MsgBox("Can't load the file. Try again.")
        End Try
    Else
        MsgBox("Chat can't seem to find the chat file. Make sure it is called Chat_Server.txt and it is on an accessible network.")
    End If

End Sub

Sub Server_Text_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Server_Text.TextChanged

    Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)    'Places the text from Chat_Server.txt into the main textbox

End Sub

Sub Chat_Text_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Chat_Text.TextChanged

    Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)    'Places the text from Chat_Server.txt into the main textbox

End Sub
Sub Chat_Text_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Chat_Text.KeyUp   'Another textbox that holds the text that the user wants to send to the Chat_Server.txt

    If e.KeyCode = Keys.Enter Then  'When the user presses enter the send button is clicked
        Send.PerformClick()
        Chat_Text.Clear()
        Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)    'Places the text from Chat_Server.txt into the main textbox
    End If

End Sub

Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click

    My.Computer.FileSystem.WriteAllText(file_name, vbCrLf & Chat_Text.Text, True)   'When send button clicked, write all contents of Client_Text to the Chat_Server.txt
    Chat_Text.Clear()
    Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)    'Places the text from Chat_Server.txt into the main textbox


End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)    'Every 100 miliseconds, places the text from Chat_Server.txt into the main textbox

End Sub
End Class

如果您能对此提供帮助,我将不胜感激,这已经困扰了我好几个星期了。

谢谢, 08robertsj

4

2 回答 2

2

就像另一个答案所说,您需要删除该行。您还需要正确创建文件并在之后处理(以便释放句柄)。此外,将您的计时器设置为较低的值,例如 1000 毫秒。我还创建了一个布尔变量来检查是否在计时器读取文件之前建立了连接。请记住检查您的计时器是否已在设置中启用。

这是代码:

Imports System.IO

Public Class Form1

    Dim file_name As String
    Dim connected As Boolean

    Sub Chat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Sub Create_Server_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Create_Server.Click 'This is a button on the form that get the user to choose a location to save the Chat_Server.txt

        SaveFileDialog1.ShowDialog()    'Save the file
        'SaveFileDialog1.OpenFile()
        file_name = SaveFileDialog1.FileName()  'Get the file path
        SaveFileDialog1.Dispose()
        File.Create(file_name).Dispose()
        MsgBox("Server file made.")
        connected = True

    End Sub

    Sub Connect_Server_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect_Server.Click   'Another button on the form that gets the user to choose the Chat_Server.txt

        OpenFileDialog1.ShowDialog()    'Open the file
        file_name = OpenFileDialog1.FileName()
        Dim fileExists As Boolean   'Tests whether the file existx and whether it works or not
        fileExists = My.Computer.FileSystem.FileExists(file_name)
        If fileExists = True Then
            Try
                Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)
            Catch ex As Exception
                MsgBox("Can't load the file. Try again.")
            End Try
        Else
            MsgBox("Chat can't seem to find the chat file. Make sure it is called Chat_Server.txt and it is on an accessible network.")
        End If

    End Sub

    Sub Server_Text_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Server_Text.TextChanged

        Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)    'Places the text from Chat_Server.txt into the main textbox

    End Sub

    Sub Chat_Text_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Chat_Text.TextChanged

        Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)    'Places the text from Chat_Server.txt into the main textbox

    End Sub
    Sub Chat_Text_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Chat_Text.KeyUp   'Another textbox that holds the text that the user wants to send to the Chat_Server.txt

        If e.KeyCode = Keys.Enter Then  'When the user presses enter the send button is clicked
            Send.PerformClick()
            Chat_Text.Clear()
            Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)    'Places the text from Chat_Server.txt into the main textbox
        End If

    End Sub

    Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click

        My.Computer.FileSystem.WriteAllText(file_name, vbCrLf & Chat_Text.Text, True)   'When send button clicked, write all contents of Client_Text to the Chat_Server.txt
        Chat_Text.Clear()
        Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)    'Places the text from Chat_Server.txt into the main textbox


    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If connected Then
            Server_Text.Text = My.Computer.FileSystem.ReadAllText(file_name)    'Every 100 miliseconds, places the text from Chat_Server.txt into the main textbox
        End If

    End Sub
End Class

如果您想制作一个合适的程序,您应该查看 VB.NET 中的 TCP 示例。这是一个教程: http: //www.codeproject.com/Articles/38914/A-TCP-IP-Chat-Program

于 2013-10-07T20:28:21.233 回答
1

不要打开文件 ( SaveFileDialog1.OpenFile())。这会导致 WriteAllText 失败,因为 WriteAllText 会自动打开和关闭文件。

于 2013-10-07T20:12:09.317 回答