📄 filedirserver.vb
字号:
Imports Microsoft.VisualBasic
Imports System.io
Imports System.Data
Imports System.Web
Public Class FileDirServer
'获取用户目录的大小,有问题,当目录下没有文件只有目录时,统计有误
Public Shared Function GetDirectorySize(ByVal thePath As String) As Long
Dim dirSize As Long
Dim dir As DirectoryInfo = New DirectoryInfo(thePath)
' Add the size of each file
Dim theFile As FileInfo
If dir.GetFiles.Length <> 0 Then
For Each theFile In dir.GetFiles()
dirSize = dirSize + theFile.Length
' Add the size of each subdirectory, retrieved by
' recursively calling this same function
Dim subDir As DirectoryInfo
For Each subDir In dir.GetDirectories
dirSize = dirSize + GetDirectorySize(subDir.FullName)
Next
Next
Else
Dim subDir As DirectoryInfo
For Each subDir In dir.GetDirectories
dirSize = dirSize + GetDirectorySize(subDir.FullName)
Next
End If
Return dirSize
End Function
Public Shared Function GetDirectorySize(ByVal DirInfo As DirectoryInfo) As Long
Dim dirSize As Long
' Dim dir As DirectoryInfo = New DirectoryInfo(thePath)
' Add the size of each file
Dim theFile As FileInfo
If DirInfo.GetFiles.Length <> 0 Then
For Each theFile In DirInfo.GetFiles()
dirSize = dirSize + theFile.Length
' Add the size of each subdirectory, retrieved by
' recursively calling this same function
Dim subDir As DirectoryInfo
For Each subDir In DirInfo.GetDirectories
dirSize = dirSize + GetDirectorySize(subDir)
Next
Next
Else
Dim subDir As DirectoryInfo
For Each subDir In DirInfo.GetDirectories
dirSize = dirSize + GetDirectorySize(subDir)
Next
End If
Return dirSize
End Function
'获取用户目录中文件的个数,同时获取用户目录下所有目录的个数
Public Shared Function GetFileCount(ByVal thePath As String, ByRef dirCount As Long) As Long
Dim fileCount As Long
' Dim dirCount As Long
Dim dir As DirectoryInfo = New DirectoryInfo(thePath)
' Add the size of each file
fileCount = fileCount + dir.GetFiles().Length
' Add the size of each subdirectory, retrieved by
' recursively calling this same function
Dim subDir As DirectoryInfo
For Each subDir In dir.GetDirectories
dirCount += 1
fileCount = fileCount + GetFileCount(subDir.FullName, dirCount)
Next
Return fileCount
End Function
'返回以兆为单位的大小
Public Shared Function FormatSize(ByVal fileSize As Double) As String
If fileSize < 1024 Then
Return String.Format("{0:N0} B", fileSize)
ElseIf (fileSize < 1024 * 1024) Then
Return String.Format("{0:N2} KB", fileSize / 1024)
Else
Return String.Format("{0:N2} MB", fileSize / (1024 * 1024))
End If
End Function
'根据扩展名,到xml文件中查找该扩展名,找到即为文本文件Server.MapPath("~/XML/TextExtend.xml")
Public Shared Function IsTextFile(ByVal Extension As String, ByVal CompareFile As String) As Boolean
Dim ds As New DataSet
If File.Exists(CompareFile) Then
ds.ReadXml(CompareFile)
Else
'"~/XML/TextExtend.xml"文件不存在则建立该文件
Dim dt As DataTable = ds.Tables.Add("FileExtension")
dt.Columns.Add("Name")
'建立一行数据
Dim Value() As String = {".txt"}
dt.Rows.Add(Value)
ds.WriteXml(CompareFile)
End If
Dim dv As DataView
dv = ds.Tables("FileExtension").DefaultView
dv.Sort = "Name"
Dim RowIndex As Integer = dv.Find(Extension)
If RowIndex > -1 Then '找到
Return True
Else
Return False
End If
End Function
'根据文件名返回该文件类型的图标文件名,例d:\hj\jsjdjks.doc,返回~\filetype\doc.gif
Public Shared Function GetGifByFileName(ByVal FilePath As String) As String
'根据FileName获得扩展名
Dim tempGif As String
Dim Index As Integer
tempGif = FilePath
Index = tempGif.LastIndexOf(".")
'没有扩展名时,index=-1
If Index = -1 Then
tempGif = "~/filetype/smileface.gif"
Else
tempGif = Mid(tempGif, Index + 2) & ".gif"
tempGif = "~/filetype/" & tempGif
End If
Return tempGif
End Function
'从全路径中提取不含路径信息的文件名
Public Shared Function GetFileNameFromPath(ByVal thePath As String) As String
'Dim Index As Integer
'Index = thePath.LastIndexOf("\")
'If Index > 0 Then
' Return Mid(thePath, Index + 2)
'End If
'Index = thePath.LastIndexOf(":")
'If Index > 0 Then
' Return Mid(thePath, Index + 2)
'End If
'Return Nothing
Return Path.GetFileName(thePath)
End Function
'根据登陆用户,从配置文件web.config获取网络硬盘目录,返回物理用户目录
'Public Shared Function GetUserDir(ByVal UserName As String) As String
' ' Dim UserDirCookie As New HttpCookie("UserDir")
' If IsNothing(Data.C_ServerDir) Or Data.C_ServerDir = "" Then
' ' UserDirCookie("UserDir") = "FALSE"
' Return String.Empty
' Else
' '判断目录后面有没有“\”,目录后就加上
' If Not Data.C_ServerDir.EndsWith("\") Then
' Data.C_ServerDir &= "\"
' End If
' '对目录进行编码
' '将服务目录存储在会话中
' ' HttpContext.Current.Session("ServerDir") = Data.C_ServerDir
' '存贮根目录在会话中
' Return Data.C_ServerDir & Trim(UserName)
' 'UserDirCookie("UserDir") = Server.UrlEncode(Data.C_ServerDir & Trim(UserName))
' End If
' ' Response.Cookies.Add(UserDirCookie)
'End Function
'根据登陆用户,从配置文件web.config获取网络硬盘目录,返回物理用户目录
Public Shared Function GetUserDir(ByVal User As String) As String
' Dim UserDirCookie As New HttpCookie("UserDir")
If IsNothing(Data.C_ServerDir) Or Data.C_ServerDir = "" Then
' UserDirCookie("UserDir") = "FALSE"
Return String.Empty
Else
'判断目录后面有没有“\”,目录后就加上
If Not Data.C_ServerDir.EndsWith("\") Then
Data.C_ServerDir &= "\"
End If
'对目录进行编码
'将服务目录存储在会话中
' HttpContext.Current.Session("ServerDir") = Data.C_ServerDir
'存贮根目录在会话中
Return Data.C_ServerDir & Trim(User)
'UserDirCookie("UserDir") = Server.UrlEncode(Data.C_ServerDir & Trim(UserName))
End If
' Response.Cookies.Add(UserDirCookie)
End Function
'返回去除服务目录后的路径
Public Shared Function ShowMapPath(ByVal FullPath As String) As String
Try
Dim strPath As String = Mid(FullPath, Len(Data.C_ServerDir))
If Left(strPath, 1) = "\" Then
Return strPath
Else
Return "\" + strPath
End If
Catch ex As Exception
Return Nothing
End Try
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -