WoodCoal.Base.Files.Utility
本文最后更新时间:2009/03/08
- '--------------------------------------------------
- '
- ' 木炭通用扩展库 - 基础库
- '
- ' namespace: WoodCoal.Base.Files.Utility
- ' author: 木炭(WoodCoal)
- ' homepage: http://www.woodcoal.cn/
- ' memo: 与 Files 相关的函数扩展
- ' release: 2009-03-08
- '
- '--------------------------------------------------
- Namespace Files
- Public Class Utility
- ''' <summary>获取当前的根目录</summary>
- Public Shared ReadOnly Property Root() As String
- Get
- Dim appRoot As String = System.AppDomain.CurrentDomain.BaseDirectory
- 'If Web.HttpContext.Current Is Nothing Then
- 'appRoot = Reflection.Assembly.GetExecutingAssembly.Location
- 'Else
- 'appRoot = Web.HttpRuntime.AppDomainAppPath
- 'End If
- 'appRoot = appRoot.Substring(0, appRoot.LastIndexOf("\"))
- If Not appRoot.EndsWith("\") Then appRoot &= "\"
- Return appRoot
- End Get
- End Property
- ''' <summary>获取文件实际路径</summary>
- ''' <param name="Path">原路径</param>
- ''' <param name="Created">不存在时是否创建</param>
- Public Shared ReadOnly Property TruePath(ByVal Path As String, Optional ByVal Created As Boolean = False) As String
- Get
- If String.IsNullOrEmpty(Path) Then
- Return Root
- Else
- If Path.Substring(1, 1) <> ":" Then Path = Root & Path
- Path = Path.Replace("/", "\")
- Path = Path.Replace("\\", "\")
- If Created Then
- Dim Dir As String = Path.Substring(0, Path.LastIndexOf("\"))
- If Not IO.Directory.Exists(Dir) Then IO.Directory.CreateDirectory(Dir)
- End If
- Return Path
- End If
- End Get
- End Property
- ''' <summary>保存文件内容,不存在的文件自动创建</summary>
- ''' <param name="Path">路径</param>
- ''' <param name="Body">内容</param>
- Public Shared Function Save(ByVal Path As String, ByVal Body As String, Optional ByVal Empty As Boolean = False) As Boolean
- Try
- If Not String.IsNullOrEmpty(Path) Then
- Path = TruePath(Path)
- If IO.File.Exists(Path) Then
- If Not Empty Then Body &= IO.File.ReadAllText(Path)
- Else
- Dim Dir As String = Path.Substring(0, Path.LastIndexOf("\"))
- If Not IO.Directory.Exists(Dir) Then IO.Directory.CreateDirectory(Dir)
- End If
- 'If Empty Then
- 'IO.File.WriteAllText(Path, String.Empty)
- 'Else
- IO.File.WriteAllText(Path, Body)
- 'End If
- End If
- Return True
- Catch ex As Exception
- Return False
- End Try
- End Function
- ''' <summary>读取文件内容,不存在的文件自动创建</summary>
- Public Shared Function Read(ByVal Path As String, Optional ByVal AutoCreated As Boolean = False) As String
- Try
- If Not String.IsNullOrEmpty(Path) Then
- Path = TruePath(Path)
- If IO.File.Exists(Path) Then
- Return IO.File.ReadAllText(Path)
- Else
- If AutoCreated Then
- Dim Dir As String = Path.Substring(0, Path.LastIndexOf("\"))
- If Not IO.Directory.Exists(Dir) Then IO.Directory.CreateDirectory(Dir)
- IO.File.WriteAllText(Path, "")
- End If
- End If
- End If
- Catch ex As Exception
- End Try
- Return ""
- End Function
- End Class
- End Namespace
· 本文由 木炭 发布在《激情燃烧的木炭》 上,原文地址为:http://www.woodcoal.cn/project/dll-base/200938-17570-556.html(转载请保留本信息、全文内容和链接)
发表评论