首先在代码最前面导入根类
Imports System.Text
Imports System.IO.Ports
然后定义一个全局的log文件路径和名字,字符串型
Private strFileName As String = ""
Private strPath As String = ""
在窗口load事件中加入代码
With SaveFileDialog1
.DefaultExt = "txt"
.FileName = strFileName
.Filter = "Text Documents (*.txt) | *.txt | All Files (*.*) | *.* "
.FilterIndex = 1
.OverwritePrompt = True
.Title = "Create or Select log file"
End With
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
strFileName = SaveFileDialog1.FileName ''获取文件路径名字
&strPath = System.IO.Path.GetDirectoryName(strFileName)‘获取文件夹路径
Else
Me.Close()
End If
最后写一个保存log的函数,以便调用,其中加入以下代码句:
My.Computer.FileSystem.WriteAllText(strFileName, log_str, True)‘log_str是字符串,保存成文本档案
'My.Computer.FileSystem.WriteAllBytes(strFileName, Read_buf, True)‘Read_buf是字节数组,保存成二进制档案
完成。