File.Create无法在asp.net中运行

时间:2013-08-19 09:23:47

标签: asp.net vb.net

这可能有点滑稽,但我面临一些愚蠢的问题。

我正在使用

File.Create(ConfigurationManager.AppSettings("LogFilePath1")).Dispose()

Using writer As StreamWriter = File.AppendText(context.Current.Server.MapPath(ConfigurationManager.AppSettings("LogFilePath1")))

当我使用它时,给我的错误是“找不到路径的一部分”

如果将路径存储在字符串中,则其工作正常,否则会产生错误。

供我参考,我正在复制我的功能代码片段。

请帮忙

由于

Public Shared Function LogErrorToLogFile(ByVal ee As Exception, ByVal userFriendlyError As String) As String
        Try
               Dim path As String = context.Current.Server.MapPath(ConfigurationManager.AppSettings("LogFilePath1"))
            ' check if directory exists
            If Not Directory.Exists(path) Then
                'Directory.CreateDirectory(path)
                Directory.CreateDirectory(context.Current.Server.MapPath(ConfigurationManager.AppSettings("LogFilePath1")))
            End If
            path = path & DateTime.Today.ToString("dd-MMM-yy") & ".log"
            ' check if file exist
            If Not File.Exists(path) Then
                File.Create(path).Dispose()
                'File.Create(context.Current.Server.MapPath(ConfigurationManager.AppSettings("LogFilePath1"))).Dispose()
                File.Create(ConfigurationManager.AppSettings("LogFilePath1")).Dispose()
            End If
            ' log the error now
            Using writer As StreamWriter = File.AppendText(path)
                'Using writer As StreamWriter = File.AppendText(context.Current.Server.MapPath(ConfigurationManager.AppSettings("LogFilePath1")))
              writer.WriteLine(HttpUtility.HtmlEncode(vbCr & vbLf & "Log written at : " & DateTime.Now.ToString() & vbCr & vbLf & "Error occured on page : " & context.Current.Request.Url.ToString() & vbCr & vbLf & vbCr & vbLf & "Here is the actual error :" & vbLf & ee.ToString()))
                writer.WriteLine("==========================================")
                writer.Flush()
                writer.Close()
            End Using
            Return userFriendlyError
        Catch
            Throw
        End Try
    End Function

2 个答案:

答案 0 :(得分:1)

如果使用功能代码段中的注释行更改当前行,则会更改逻辑,因为在使用Path时,会在路径中添加文件名[dd-MMM-yy] .log -串。但是当您直接使用ConfigurationManager.AppSettings(“LogFilePath1”)时,不要使用该添加项!

答案 1 :(得分:0)

我假设您没有使用带斜杠的文件夹路径(例如:@“C:\ FolderName \”),这就是您收到错误的原因。

相关问题