1、编写日志
2、本地文件的读取和写入
3、Base64与图片的互相转换
Imports System.IO Imports System.Text Public Class Cls_File #Region "编写日志" '根目录 Public Shared strCurrentPath As String 'Cls_File.WriteLog("FrmTask.Button1_Click", rtf.Text) Public Sub WriteLog(ByVal strModuelName As String, ByVal strDescription As String) Try '得到根目录 strCurrentPath = System.Windows.Forms.Application.StartupPath + "\AwLog\" Dim strDt As String = Date.Now.ToString("yyyy-MM-dd HH:mm:ss") '判断目录是否存在,存在 If Directory.Exists(strCurrentPath) = True Then '得到文件目录 Dim strFilePath As String = strCurrentPath + DateTime.Today.ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo) + ".log" Dim sw As New StreamWriter(strFilePath, True) sw.WriteLine(strDt + ":" + " " + strModuelName) sw.WriteLine(strDescription) sw.Close() Else '根目录不存在 Dim dirPathInfo As DirectoryInfo = Directory.CreateDirectory(strCurrentPath) dirPathInfo.Attributes = FileAttributes.Archive '设置属性为存档 创建文件 Dim sw As New StreamWriter(strCurrentPath + DateTime.Today.ToString("yyyy-MM-dd") + ".log", True) sw.WriteLine(strDt + ":" + " " + strModuelName) sw.WriteLine(strDescription) sw.Close() End If Catch ex As Exception MsgBox(ex.Message.ToString, MsgBoxStyle.Exclamation, "提示") End Try End Sub #End Region #Region "本地文件读写字符串" Function ReadFile(ByVal FileName As String) Dim MyReader = New StreamReader(FileName, Encoding.UTF8) Dim MyText = MyReader.ReadToEnd() MyReader.Close() Return MyText End Function Function WriteFile(ByVal FileName As String, ByVal strTmp As String) Dim file As New System.IO.StreamWriter(FileName) file.WriteLine(strTmp) file.Close() Return "ok" End Function #End Region #Region "Base64与图片的互转" ''' <summary> ''' 将Base64转为图片 ''' </summary> ''' <param name="base64"></param> ''' <returns></returns> Public Shared Function Base64ToImage(ByVal base64 As String) As Image Dim bases As String = base64 Dim bytes As Byte() = Convert.FromBase64String(bases) Dim memStream As New MemoryStream(bytes) Dim bit As New Bitmap(memStream) Dim img As Image = bit Return img End Function ''' <summary> ''' 将图片转为Base64 ''' </summary> ''' <param name="image"></param> ''' <returns></returns> Public Shared Function ImageToBase64(ByVal Image As Image) As String Try Dim ms As New MemoryStream() Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg) Dim arr As Byte() = New Byte(ms.Length - 1) {} ms.Position = 0 ms.Read(arr, 0, CType(ms.Length, Integer)) ms.Close() Dim strbaser64 As [String] = Convert.ToBase64String(arr) Return strbaser64 Catch ex As Exception ImageToBase64 = "" End Try End Function #End Region