Wil*_*rds 4 vb6 vba windows-server-2008
我有这个函数写入我的日志文件.它默默地失败了.因为在此函数中没有引发错误,它只是无法创建或写入文件.我正在写信给你%TEMP%\myappname.log.它也失败了%USERPROFILE%\Desktop\myappname.log.服务器是Windows Server 2008 R2 Standard.我用其他程序写入应用程序文件夹时遇到了这种情况,因此转移到写入%TEMP%目录并解决了这个问题.但是这个系统甚至不会让我写到%TEMP%目录.是的我尝试以管理员身份运行,但没有帮助.在这种情况下,%TEMP%解析ExpandEnvironmentStrings为,C:\Users\sa\AppData\Local\Temp\2
所以g_strLogPath是C:\Users\sa\AppData\Local\Temp\2\myappname.log.
Public Function LogMessage(ByVal strInput As String, Optional ByVal blnDate As Boolean = False) As Boolean
Dim intFileNumber As Integer
On Error GoTo ErrorHandler
If g_lngLogLevel <> 1 Then
Exit Function
End If
If Len(g_strLogPath) = 0 Then
SetLogPath
End If
If blnDate Then
strInput = Format(Now, cstrLogDateFormat) & " : " & strInput
End If
intFileNumber = FreeFile
Open g_strLogPath For Append As #intFileNumber
Print #intFileNumber, strInput
Close #intFileNumber
LogMessage = True
Exit Function
ErrorHandler:
MsgBox _
"Error: " & Err.Number & vbCrLf & _
"Location: Module1.LogMessage" & vbCrLf & _
"Line: " & Erl & vbCrLf & _
Err.Description, vbExclamation + vbOKOnly
End Function
Run Code Online (Sandbox Code Playgroud)
试试这个
Sub GetTmpPath()
'This will give the Temp Path
MsgBox IIf(Environ$("tmp") <> "", Environ$("tmp"), Environ$("temp"))
End Sub
Run Code Online (Sandbox Code Playgroud)
所以你可以尝试使用它
Ret = IIf(Environ$("tmp") <> "", Environ$("tmp"), Environ$("temp"))
g_strLogPath = Ret & "\Sample.Log"
Open g_strLogPath For Append As #intFileNumber
Run Code Online (Sandbox Code Playgroud)