C# 如何在Nunit测试中归档.Exists

C# 如何在Nunit测试中归档.Exists,c#,vb.net,nunit,C#,Vb.net,Nunit,我有一种方法,可以在对文件内容执行某些操作之前测试文件是否存在,如下所示: Private Sub AddHeaderToLogFile() ''#Only if file does not exist If Not File.Exists(_logPath) Then Dim headerLogger As Logger headerLogger = LogManager.GetLogger("HeaderLogger") ''#

我有一种方法,可以在对文件内容执行某些操作之前测试文件是否存在,如下所示:

Private Sub AddHeaderToLogFile()
    ''#Only if file does not exist
    If Not File.Exists(_logPath) Then
        Dim headerLogger As Logger
        headerLogger = LogManager.GetLogger("HeaderLogger")
        ''#Use GlobalDiagnosticContext in 2.0, GDC in pre-2.0
        NLog.GlobalDiagnosticsContext.Set("appName", _appName)
        NLog.GlobalDiagnosticsContext.Set("fileVersion", _fileVersion)
        NLog.GlobalDiagnosticsContext.Set("logId", 0)
        headerLogger.Info("")
    End If
End Sub
其思想是,如果该文件不存在,则通过调用NLog logger实例生成该文件,此时将创建该文件并插入指定的头信息。该方法从应用程序本身就可以很好地工作,但是我有一个简单的NUnit测试,它实现了一个测试方法来验证文件是否按预期创建和填充。当我逐步使用调试器时,我发现“\u logPath”设置为:

D:\Documents and Settings\TE602510\Local Settings\Temp\nunit20\ShadowCopyCache\4288\u 634286300896838506\Tests\uuu937845265\assembly\dl3\7cdfe61a\aa18c98d\u f0a1cb01\logs\2010-12-22.log

不幸的是,尽管文件确实存在,但对file.Exists的调用返回false。从前面查看的配置路径来看,上述路径对于这些NUnit测试似乎是正确的。有没有人知道这里发生了什么,我需要做什么才能得到想要的结果?根据XP文件系统,日志文件的路径名为:

D:\Documents and Settings\TE602510\My Documents\u VSSWorkArea\PsalertsIp\Tests\bin\Debug\logs

问候


Paul J.

这可能是一个权限问题。从,
File.Exists(路径)
返回:

true如果调用方具有所需的权限,并且路径包含现有文件的名称;否则,false。如果路径是空引用(在Visual Basic中为空)、无效路径或零长度字符串,则此方法还返回false。如果调用方没有足够的权限读取指定的文件,则不会引发异常,并且无论路径是否存在,该方法都会返回false

编辑 这里有两种方法将尝试获取目录/文件的ACL。调用NUnit的方式可能会导致权限不同

Private Shared Function DoesFileExist(ByVal file As String) As Boolean
    Try
        Dim FI As New System.IO.FileInfo(file)
        Dim AC = FI.GetAccessControl()              'This method will fail if the file is not found'
        Return True
    Catch ex As System.IO.FileNotFoundException     'FNF error should be thrown if it does not exist'
        Return False
    Catch ex As Exception
        'If you do not have permission to read the permissions this might throw, too.'
        'Of course, in that situation that means that the file exists so we should'
        'probably return True here, too. For debugging I am still throwing though.'
        Throw ex
    End Try
End Function
Private Shared Function DoesDirectoryExist(ByVal dir As String) As Boolean
    Try
        Dim DI As New System.IO.DirectoryInfo(dir)
        Dim AC = DI.GetAccessControl()               'This method will fail if the directory is not found'
        Return True
    Catch ex As System.IO.DirectoryNotFoundException 'DNF error should be thrown if it does not exist'
        Return False
    Catch ex As Exception
        'If you do not have permission to read the permissions this might throw, too.'
        'Of course, in that situation that means that the directory exists so we should'
        'probably return True here, too. For debugging I am still throwing though.'
        Throw ex
    End Try
End Function

不使用文件。存在

文件系统是易变的,所以在检查和对结果执行操作之间,您要查找的文件的存在(或不存在)可能会发生变化。此外,File.Exists不确定您是否具有有关文件的正确权限

相反,您要做的是尝试正确打开文件,如下所示:

Try
    Using fs As IO.FileStream = IO.File.Open(_logPath, FileMode.CreateNew)

    End Using
Catch ex AS IOException 
    ''# This will be thrown if the file already exists - just swallow it
    ''# If you also want to handle IOExceptions while working on the file,
    ''#  place another try/catch inside the using block
End Try

仅当文件的存在本身具有意义时,才应使用File.Exists,但该文件永远不会被实际读取或写入。例如,一些主框架系统会创建和销毁文件,以指示锁定,或发出作业完成的信号。

对我来说,这听起来像是框架的一个bug。克里斯,谢谢你的回复。这里很难看出权限是一个问题,所描述的路径属于“我的文档”文件夹结构,所讨论的文件是在运行时创建的,没有任何问题。我想这与通过NUnit生成路径的方式有关(即,不是实际的physcal路径名)…请记住,文件系统是不稳定的-这些方法应该使用过去时进行最准确的命名。DidFileExistaCoupleOfMillSecondsAgo?您可以关闭NUnit卷影复制选项(并直接从调试文件夹运行测试)-如果错误确实来自日志位置。