建立文件數據庫
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的文件,而後在寫入本地磁盤該如何作呢?dom
從數據庫讀取文件
函數
'讀取數據庫中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長度的字節ui
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
將VS中輸出的debug統一放入指定的txt文件夾中spa
Public Const LOG_PATH As String = "C:\Users\Public\Documents\ECharge\" If Not System.IO.Directory.Exists(Main.LOG_PATH) Then System.IO.Directory.CreateDirectory(Main.LOG_PATH) End If If Not System.IO.Directory.Exists(Main.LOG_PATH & "Connect Server") Then System.IO.Directory.CreateDirectory(Main.LOG_PATH & "Connect Server") End If If Not System.IO.Directory.Exists(Main.LOG_PATH & "Connect Server\Exception\") Then System.IO.Directory.CreateDirectory(Main.LOG_PATH & "Connect Server\Exception\") End If If Not System.IO.Directory.Exists(Main.LOG_PATH & "Connect Server\Exception\" & Main.serverID & "\") Then System.IO.Directory.CreateDirectory(Main.LOG_PATH & "Connect Server\Exception\" & Main.serverID & "\") End If Dim Str As New FileStream(Main.LOG_PATH & "Connect Server\Exception\" & Main.serverID & "\" & "DebugMessages.txt", System.IO.FileMode.Append) Dim DebugFile As New StreamWriter(Str) Dim Listener = New TextWriterTraceListener(DebugFile) Debug.Listeners.Add(Listener) Debug.AutoFlush = True Debug.Print("----- start debug print to file")
上述代碼將程序運行的debug放到c盤指定的目錄,並建立一個DebguMessage.txt文檔,執行後效果圖以下:debug
VB.NET建立隨機數 code
1 '產生隨機數 2 Try 3 Dim Vchar As String = "00030,0031,00032,00033,00034,00035" 4 Dim VcArray() As String = Split(Vchar, ",") '將字符串生成數組 5 Dim VNum As String = "" 6 Dim objRandom As Random = New Random 7 objRandom.Next(0, 5) 8 VNum = VNum & VcArray(Int(objRandom.Next(0, 5))) '數組從0開始讀取,後面指定讀取最大界限,防止數組越界 9 MessageBox.Show(VNum) 10 Catch ex As Exception 11 Debug.Print(ex.Message) 12 Throw ex 13 End Try
VB.NET Integer類型轉成byte類型並調用sort方法從新對元素排序 orm
Public Sub setChargerStattimeStamp(ByRef StataTimeStamp As Integer) Try chargerStatTimeStamp = BitConverter.GetBytes(StataTimeStamp) Dim sorts As List(Of Byte) = New List(Of Byte)(chargerStatTimeStamp) sorts.Sort() chargerStatTimeStamp(0) = sorts(0) chargerStatTimeStamp(1) = sorts(1) chargerStatTimeStamp(2) = sorts(2) chargerStatTimeStamp(3) = sorts(3) Catch ex As Exception Throw ex End Try End Sub
VB.NET byte免拼接高級寫法server
Public Sub setRemoteUID(ByRef byteArray() As Byte) Try 'D1 46 A3 6A 5E 08 04 00 01 D7 15 65 0B 94 3D 1D 原始UID:209701631069429 Array.Copy(byteArray, 0, Me.remoteUid, 0, UID_LENGTH) Array.Resize(Me.remoteUid, UID_LENGTH) ArrayRemove(byteArray, UID_LENGTH) Dim id As String() = Me.remoteUid.Select(Function(byt) byt.ToString("x2")).ToArray Dim uid As String = String.Join("-", id).ToUpper Me.remoteUid_Value = uid Dim id1 As String() = Me.remoteUid.Select(Function(byt) byt.ToString("x2")).ToArray Dim uid1 As String = String.Join("", id).ToUpper Meter_Reading.ByNum.Uid = Me.remoteUid_Value Meter_Reading.ByNum.vchprivateID = uid1 Debug.Print("##############remoteUid_Value=" & Me.remoteUid_Value) Debug.Print("##############vchprivateID=" & Meter_Reading.ByNum.vchprivateID) Catch ex As Exception Throw ex End Try End Sub
end