vba 在 %TEMP% 中写入文件无声无息地失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10438855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:01:38  来源:igfitidea点击:

writing file in %TEMP% fails silently

vbavb6windows-server-2008

提问by Will Rickards

I have this function which writes to my log file. It is failing silently. In that there are no errors raised in this function, it just fails to create or write to the file. I'm trying to write to %TEMP%\myappname.log. It also fails in %USERPROFILE%\Desktop\myappname.log. Server is Windows Server 2008 R2 Standard. I've encountered this with writing to the application folder with other programs so moved to writing to the %TEMP%directory and this solved it. But this system won't even let me write to the %TEMP%directory. Yes I tried running as administrator too and it didn't help. %TEMP% in this case resolves through ExpandEnvironmentStringsto C:\Users\sa\AppData\Local\Temp\2So g_strLogPath is C:\Users\sa\AppData\Local\Temp\2\myappname.log.

我有这个功能可以写入我的日志文件。它在默默地失败。因为这个函数没有引发错误,它只是无法创建或写入文件。我正在尝试写信给%TEMP%\myappname.log. 它也失败了%USERPROFILE%\Desktop\myappname.log。服务器是 Windows Server 2008 R2 标准版。我在用其他程序写入应用程序文件夹时遇到了这个问题,因此转移到写入%TEMP%目录并解决了这个问题。但是这个系统甚至不会让我写入%TEMP%目录。是的,我也尝试以管理员身份运行,但没有帮助。在这种情况下,%TEMP% 解析ExpandEnvironmentStringsC:\Users\sa\AppData\Local\Temp\2因此 g_strLogPath is 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

采纳答案by Will Rickards

The problem was loglevel checking. It never actually got to the open or print statements.

问题是日志级别检查。它实际上从未到达 open 或 print 语句。

I had three log levels defined 0 = no log 1 = everything 2 = errors only

我定义了三个日志级别 0 = 没有日志 1 = 一切 2 = 只有错误

It was set to 2 And LogError called LogMessage which checked if loglevel <> 1 and thus never ran.

它被设置为 2 并且 LogError 称为 LogMessage,它检查日志级别 <> 1 并因此从未运行。

回答by Siddharth Rout

Try this

尝试这个

Sub GetTmpPath()
    'This will give the Temp Path
    MsgBox IIf(Environ$("tmp") <> "", Environ$("tmp"), Environ$("temp"))
End Sub

So you can try using it as

所以你可以尝试使用它作为

    Ret = IIf(Environ$("tmp") <> "", Environ$("tmp"), Environ$("temp"))

    g_strLogPath = Ret & "\Sample.Log"

    Open g_strLogPath For Append As #intFileNumber

回答by wqw

Try using GetTempPathinstead of Environ$("TEMP")to get the temp folder.

尝试使用GetTempPath而不是Environ$("TEMP")获取临时文件夹。

If current user does not have write permissions in TEMP folder, lots of system components will fail too.

如果当前用户在 TEMP 文件夹中没有写权限,许多系统组件也会失败。