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