VB.Net - FileInfo.FullName - 我错过了什么吗?

发布于 2024-10-14 23:30:12 字数 1235 浏览 6 评论 0原文

我有一些代码是在我开始之前编写的(听起来像是为 The Daily WTF 提交的!),它加载一个 xml 文件进行处理(Throw 行已被简化以隐藏罪魁祸首的身份,否则是逐字的)。

Try
    docData.Load(strPath)
Catch oError As Exception
    Throw New Exception("There is a load or parse error in the xml")
End Try

oFileInfo = New FileInfo(strPath)
strFileName = oFileInfo.FullName
oFileInfo = Nothing

strFileName 在该方法中再次使用,传递给另一个方法

删除文件时

strPath 在该方法中再次使用,当从 MSDN

全名< /a>:获取目录或文件的完整路径。 (继承自 FileSystemInfo.)

当然,这只是返回 strPath 中已有的内容,并且可以用简单的

strFileName = strPath

替换,甚至删除 strFileName code> 一起使用 strPath

或者我错过了什么? FileInfo.FullName 还执行其他操作吗?

我确实认为这是一个文件存在检查,但这已经由 XmlDocument.Load 周围的 Try...Catch 处理,此外,File.Exists(strPath) 会简单得多。

I have some some code which was written before I started here (sounds like a submission for The Daily WTF!) which loads an xml file for processing (the Throw line has been simplified to hide the identity of the culprit, otherwise its verbatim).

Try
    docData.Load(strPath)
Catch oError As Exception
    Throw New Exception("There is a load or parse error in the xml")
End Try

oFileInfo = New FileInfo(strPath)
strFileName = oFileInfo.FullName
oFileInfo = Nothing

strFileName is used once more in the method, passed to another method

strPath is used once more in the method, when deleting the file

From MSDN:

FullName: Gets the full path of the directory or file. (Inherited from FileSystemInfo.)

Surely then, that is simply returning what is already in strPath and can be replaced with a simple

strFileName = strPath

Or even do away with strFileName altogether and use strPath throughout.

Or am I missing something? Does FileInfo.FullName do anything else?

I did think it was a file exists check, but that has already been taken care of by the Try...Catch around the XmlDocument.Load and besides, File.Exists(strPath) would be much simpler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔嚣张 2024-10-21 23:30:12

即使输入 strPath 是相对路径,FileInfo.FullName 也会返回完整路径。

FileInfo.FullName will return the full path even if the input strPath is a relative path.

撕心裂肺的伤痛 2024-10-21 23:30:12

@Joe 的意思是,从技术上讲,strPath 可能是相对路径。当您将其传递到 FileInfo 并检索 FullName 属性时,它会转换为绝对路径。尽管您说一切都是 UNC(并且我假设您也指的是绝对路径),但有可能在某一时刻这是用相对路径调用的。例如,此代码将输出 c:\Users\...\bin\somefile.bin

    Dim F As New System.IO.FileInfo("..\somefile.bin")
    Trace.WriteLine(F.FullName)
    Me.Close()

如果您知道您将始终处理绝对路径,那么您现在可能可以摆脱该代码。

What @Joe is saying is that technically strPath could be a relative path. When you pass that into FileInfo and retrieve the FullName property it gets converted into an absolute path. Although you says that everything is UNC (and by that I'm assuming you also mean absolute paths) its possible that at one point this was called with relative paths. For instance this code will output c:\Users\...\bin\somefile.bin

    Dim F As New System.IO.FileInfo("..\somefile.bin")
    Trace.WriteLine(F.FullName)
    Me.Close()

If you know that you'll always be dealing with absolute paths you can probably get rid of that code now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文