VB.NET程序目录下写日志
2016-02-29 11:33
447 查看
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
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
相关文章推荐
- VB.NET校验字符串函数
- VB.NET实现验证信用卡卡号
- VB.NET中TextBox的智能感知应用实例
- VB.NET获取文件默认图标的方法
- 自己写的文件操作的function和Sub vb.net dll
- VB.NET调用MySQL存储过程并获得返回值的方法
- vb.net发布水晶报表程序步骤
- vb.net读写文件代码
- VB.net常用字符和日期等函数
- C#、vb.net及SQL判断指定年份是否为闰年的方法
- 2016年2月22号工作日记
- 经典讲解VB.NET线程方法之访问数据库
- 初探宏的强大,写一个统计VB.NET代码行数的宏(下)
- 一个非常不错的业务规则管理器
- vs.net下的开发
- VB.NET中层次数据绑定TreeView的实现
- ATL CLR MFC Win32的区别
- 电话自动停复机相关技术1:键盘、鼠标模拟
- javascript的编写、调试
- 请教vs.net2005打包的问题