VB.NET程序目录下写日志
发布时间:2020-12-17 07:40:30 所属栏目:百科 来源:网络整理
导读:System.Environment.CurrentDirectory 与 Application.StartupPath 获取程序启动路径的区别: System.Environment.CurrentDirectory的含义是获取或设置当前工作路径,而Application.StartupPath是获取程序启动路径,表面上看二者没什么区别,但实际上区别大
System.Environment.CurrentDirectory与Application.StartupPath获取程序启动路径的区别:
System.Environment.CurrentDirectory的含义是获取或设置当前工作路径,而Application.StartupPath是获取程序启动路径,表面上看二者没什么区别,但实际上区别大得很。 先说前者:比如说你程序放在桌面上启动,但是中间你用了一个OpenFileDialog打开了E盘名为abc的文件夹下的某一个文件,那么CurrentDirectory就变成E:abc了,所以如果你想再获取程序启动文件夹的某一个文件就没用了,但是Application.StartupPath就不会这样了,无论你中间打开了哪个盘的文件,启动路径都是在桌面那里,一直不会变。
''' <summary> ''' 获取根目录 ''' </summary> ''' <returns></returns> ''' <remarks></remarks> Public Shared Function GetPath() As String Return Application.StartupPath 'Return System.Environment.CurrentDirectory End Function ''' <summary> ''' 写日志 ''' </summary> ''' <param name="strStyle">操作类型</param> ''' <param name="strContent">操作内容</param> ''' <remarks></remarks> Public Shared Sub WriteLog(ByVal strStyle As String,ByVal strContent As String) Dim curDate As String = CDate(Now).ToString("yyyy-MM-dd HH:mm:ss") Dim path As String = GetPath() & "log" If Not My.Computer.FileSystem.DirectoryExists(path) Then My.Computer.FileSystem.CreateDirectory(path) End If path &= "" & CDate(curDate).ToString("yyyyMMdd") & ".txt" If Not My.Computer.FileSystem.FileExists(path) Then My.Computer.FileSystem.WriteAllText(path,String.Empty,False) End If Dim strValue As String = String.Empty strValue = "******************" & curDate & "**********************" & vbCrLf strValue &= "*************操作类型[" & strStyle & "]***************" & vbCrLf strValue &= "***************错误原因***************" & vbCrLf strValue &= strContent & vbCrLf & vbCrLf & vbCrLf My.Computer.FileSystem.WriteAllText(path,strValue,True) End Sub (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |