VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • VB.NET 创建文件以及文件的读写(创建随机数)

创建文件

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

 将VS中输出的debug统一放入指定的txt文件夹中

 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文档,执行后效果图如下:

   

VB.NET创建随机数

 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方法重新对元素排序

 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免拼接高级写法

 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

 


__EOF__

作  者潇十一郎
出  处https://www.cnblogs.com/zhangxiaoyong/p/5112793.html


相关教程