VB.NET - 检查 URL 是目录还是文件 [英] VB.NET - Check if URL is a directory or file

查看:46
本文介绍了VB.NET - 检查 URL 是目录还是文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VB.NET 中,有没有办法检查 URL 是否是目录?我见过很多检查本地路径是否是目录的方法,但是远程 url 呢(即 http://website.com/foo) 我读到一些纯文本文件没有扩展名,所以我需要一个解决方案,而不是检查文件名是否包含空格或其他内容.

Is there a way, in VB.NET, to check if a URL is a directory? I've seen a lot of methods of checking if a local path is a directory but what about a remote url (i.e. http://website.com/foo) I read that some plain text files have no extension so I need a solution other than checking what if the file name contains a space or something.

推荐答案

您可以使用 FileAttributes 类:

You can use FileAttributes class:

'get the file attributes for file or directory
FileAttributes attr = File.GetAttributes("c:\\Temp")

'detect whether its a directory or file
If ((attr & FileAttributes.Directory) = FileAttributes.Directory) Then
    MessageBox.Show("Its a directory")
Else
    MessageBox.Show("Its a file")
End IF

或者您可以使用 Uri 类:

Or you can use the Uri class:

Private IsLocalPath(Byval p As String) As Boolean
  Return New Uri(p).IsFile
End Function

您可以增强此方法以包含对某些无效 URI 的支持:

You can enhance this method to include support for certain invalid URIs:

Private IsLocalPath(Byval p As String) As Boolean
  If (p.StartsWith("http:\\")) Then      
    Return False
  End IF

  Return New Uri(p).IsFile
End Function

这篇关于VB.NET - 检查 URL 是目录还是文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆