VB.Net - FileInfo.FullName - 我错过了什么吗?
我有一些代码是在我开始之前编写的(听起来像是为 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:
当然,这只是返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使输入 strPath 是相对路径,
FileInfo.FullName
也会返回完整路径。FileInfo.FullName
will return the full path even if the input strPath is a relative path.@Joe 的意思是,从技术上讲,strPath 可能是相对路径。当您将其传递到
FileInfo
并检索FullName
属性时,它会转换为绝对路径。尽管您说一切都是 UNC(并且我假设您也指的是绝对路径),但有可能在某一时刻这是用相对路径调用的。例如,此代码将输出c:\Users\...\bin\somefile.bin
如果您知道您将始终处理绝对路径,那么您现在可能可以摆脱该代码。
What @Joe is saying is that technically
strPath
could be a relative path. When you pass that intoFileInfo
and retrieve theFullName
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 outputc:\Users\...\bin\somefile.bin
If you know that you'll always be dealing with absolute paths you can probably get rid of that code now.