创建文件
Dim strFile As String = String.Format("C:\ErrorLog.txt", DateTime.Today.ToString("dd-MMM-yyyy")) File.AppendAllText(strFile, String.Format("Error Message in Occured at-- {0}{1}", DateTime.Now, Environment.NewLine))
第一句话的意思是在c盘下面创建一个ErrorLog.txt的文本文档,第二句话的意思是,在这个文本文档中写入"Error Message in Occured at--后面接当前时间,每打印一句就换一行。
上面是直接从本地磁盘写,那么,如果从数据库中要读取后缀.bin的文件,然后在写入本地磁盘该如何做呢?
从数据库读取文件
‘读取数据库中bin文件 Dim blobFiles As Integer = 0 Try System.IO.Directory.CreateDirectory("C:\var\") myCommand.CommandText = "select blobFile from evnet.join_server_firmware_version order by dttDateTime desc" ‘从数据库中读取存放文件字段按时间最新 Dim dr As MySqlDataReader = myCommand.ExecuteReader() dr.Read() Dim b(dr.GetBytes(blobFiles, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte ‘声明b数组存放读取到的dr dr.GetBytes(blobFiles, 0, b, 0, b.Length) ‘将b读取到的数存放在blobFiles中 dr.Close() conn.Close() conn.Open() myCommand.CommandText = "select vchfimware_versionName from evnet.join_server_firmware_version order by dttDateTime desc" VersionName = myCommand.ExecuteScalar conn.Close() If System.IO.File.Exists("C:\var\" + VersionName + ".txt") Then ‘判断当前c盘是否存储有该文件夹 Else Dim fs12 As New System.IO.FileStream("C:\var\" + VersionName + ".txt ", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite) ‘无则创建改文件 fs12.Write(b, blobFiles, b.Length) ‘向创建的文件中开始写,一次性写完 fs12.Close() ‘关闭读写操作,以免引发异常 fs12.Dispose() End If ‘====================== Catch ex As Exception Debug.Print("#######################" & ex.StackTrace) Debug.Print("########" & ex.Message) Throw ex End Try
每次只读取2014长度的字节
Dim fs As New System.IO.FileStream("C:\var\" + VersionName + ".txt", IO.FileMode.Open, IO.FileAccess.Read) Dim buffer(1023) As Byte Dim re As New System.IO.BinaryReader(fs) Dim numdouble As Double = (fs.Length / 1024) num = Math.Ceiling(numdouble) ‘此函数用于判断是否有小数,有小数将自动取整比如:1.2将会取2,1.6将会取2 fs.Seek(i * ByNum.c, SeekOrigin.Begin) re.Read(buffer, 0, 1024) ‘表示从0位开始读1024位 如果想取分组包长度可用 buffer.Length 如果想取数据则直接可取buffer
end
时间: 2024-10-13 11:49:10