项目 - 通用扩展库 - 基础库 - WoodCoal.Base.Files.Utility

WoodCoal.Base.Files.Utility

本文最后更新时间:2009/03/08

  1. '--------------------------------------------------     
  2. '   
  3. '   木炭通用扩展库 - 基础库   
  4. '   
  5. '   namespace: WoodCoal.Base.Files.Utility
  6. '   author: 木炭(WoodCoal)   
  7. '   homepage: http://www.woodcoal.cn/   
  8. '   memo: 与 Files 相关的函数扩展   
  9. '   release: 2009-03-08
  10. '   
  11. '-------------------------------------------------- 
  12.  
  13. Namespace Files
  14.     Public Class Utility
  15.         ''' <summary>获取当前的根目录</summary>
  16.         Public Shared ReadOnly Property Root() As String
  17.             Get
  18.                 Dim appRoot As String = System.AppDomain.CurrentDomain.BaseDirectory
  19.                 'If Web.HttpContext.Current Is Nothing Then
  20.                 'appRoot = Reflection.Assembly.GetExecutingAssembly.Location
  21.                 'Else
  22.                 'appRoot = Web.HttpRuntime.AppDomainAppPath
  23.                 'End If
  24.                 'appRoot = appRoot.Substring(0, appRoot.LastIndexOf("\"))
  25.                 If Not appRoot.EndsWith("\") Then appRoot &= "\"
  26.                 Return appRoot
  27.             End Get
  28.         End Property
  29.  
  30.         ''' <summary>获取文件实际路径</summary> 
  31.         ''' <param name="Path">原路径</param>
  32.         ''' <param name="Created">不存在时是否创建</param>
  33.         Public Shared ReadOnly Property TruePath(ByVal Path As StringOptional ByVal Created As Boolean = FalseAs String
  34.             Get
  35.                 If String.IsNullOrEmpty(Path) Then
  36.                     Return Root
  37.                 Else
  38.                     If Path.Substring(1, 1) <> ":" Then Path = Root & Path
  39.                     Path = Path.Replace("/""\")
  40.                     Path = Path.Replace("\\", "\")
  41.  
  42.                     If Created Then
  43.                         Dim Dir As String = Path.Substring(0, Path.LastIndexOf("\"))
  44.                         If Not IO.Directory.Exists(Dir) Then IO.Directory.CreateDirectory(Dir)
  45.                     End If
  46.  
  47.                     Return Path
  48.                 End If
  49.             End Get
  50.         End Property
  51.  
  52.         ''' <summary>保存文件内容,不存在的文件自动创建</summary>
  53.         ''' <param name="Path">路径</param>
  54.         ''' <param name="Body">内容</param>
  55.         Public Shared Function Save(ByVal Path As StringByVal Body As StringOptional ByVal Empty As Boolean = FalseAs Boolean
  56.             Try
  57.                 If Not String.IsNullOrEmpty(Path) Then
  58.                     Path = TruePath(Path)
  59.  
  60.                     If IO.File.Exists(Path) Then
  61.                         If Not Empty Then Body &= IO.File.ReadAllText(Path)
  62.                     Else
  63.                         Dim Dir As String = Path.Substring(0, Path.LastIndexOf("\"))
  64.                         If Not IO.Directory.Exists(Dir) Then IO.Directory.CreateDirectory(Dir)
  65.                     End If
  66.  
  67.                     'If Empty Then
  68.                     'IO.File.WriteAllText(Path, String.Empty)
  69.                     'Else
  70.                     IO.File.WriteAllText(Path, Body)
  71.                     'End If
  72.                 End If
  73.  
  74.                 Return True
  75.             Catch ex As Exception
  76.                 Return False
  77.             End Try
  78.         End Function
  79.  
  80.         ''' <summary>读取文件内容,不存在的文件自动创建</summary>
  81.         Public Shared Function Read(ByVal Path As StringOptional ByVal AutoCreated As Boolean = FalseAs String
  82.             Try
  83.                 If Not String.IsNullOrEmpty(Path) Then
  84.                     Path = TruePath(Path)
  85.  
  86.                     If IO.File.Exists(Path) Then
  87.                         Return IO.File.ReadAllText(Path)
  88.                     Else
  89.                         If AutoCreated Then
  90.                             Dim Dir As String = Path.Substring(0, Path.LastIndexOf("\"))
  91.                             If Not IO.Directory.Exists(Dir) Then IO.Directory.CreateDirectory(Dir)
  92.                             IO.File.WriteAllText(Path, "")
  93.                         End If
  94.                     End If
  95.                 End If
  96.             Catch ex As Exception
  97.             End Try
  98.  
  99.             Return ""
  100.         End Function
  101.     End Class
  102. End Namespace

· 本文由 木炭 发布在《激情燃烧的木炭》 上,原文地址为:http://www.woodcoal.cn/project/dll-base/200938-17570-556.html(转载请保留本信息、全文内容和链接)

发表评论

已经有 0 位朋友发表了对《WoodCoal.Base.Files.Utility》的看法
 
登录名:  密码:   登录  注册
评论: 
User:
Contact:
验证码:  
  [Ctrl+Enter]

快捷导航

推荐文章