Guest User

Untitled

a guest
Apr 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.86 KB | None | 0 0
  1. '--- Austin's Read/Write System v0.02 ---
  2. '--- 11/29/2011-11/30/2011 ---
  3. '
  4. '... Functions ...
  5. 'Write(File, Key, Data)
  6. ' -> Writes Data to the File under the Key specified
  7. 'Read(File, Key) [return: string]
  8. ' -> Reads File and returns data under the Key specified
  9. 'KeyExists(File, Key) [return: True/False]
  10. ' -> Checks if the Key exists in the File specified
  11.  
  12. Option Explicit On
  13. Option Infer On
  14. Option Strict On
  15.  
  16. Imports System.IO
  17.  
  18. Module ReadWrite
  19.  
  20.     Public Sub Write(ByVal FileName As String, ByVal Key As String, ByVal Data As String)
  21.         Dim i, a, x As Integer, dir As String, FileData As String = Nothing
  22.  
  23.         While i <> FileName.Length
  24.             If FileName(i) = "/" Then
  25.                 dir = FileName.Substring(0, FileName.IndexOf("/", i))
  26.                 x = i + 1
  27.  
  28.                 If Directory.Exists(dir) = False Then Directory.CreateDirectory(dir)
  29.             End If
  30.  
  31.             i += 1
  32.         End While
  33.  
  34.         If System.IO.File.Exists(FileName) = True Then
  35.             Dim file2 As String() = IO.File.ReadAllLines(FileName)
  36.  
  37.             For a = 0 To file2.GetUpperBound(0)
  38.                 If file2(a).Length > Key.Length Then
  39.                     If file2(a).Substring(0, Key.Length) & ":" <> Key & ":" Then
  40.                         FileData = FileData & file2(a) & vbNewLine
  41.                     End If
  42.                 Else
  43.                     FileData = FileData & file2(a) & vbNewLine
  44.                 End If
  45.             Next
  46.         End If
  47.  
  48.         Dim file As New StreamWriter(FileName)
  49.  
  50.         file.Write(FileData & Key & ":" & Data)
  51.         file.Close()
  52.     End Sub
  53.  
  54.     Public Function Read(ByVal FileName As String, ByVal Key As String) As String
  55.         Dim a As Integer
  56.  
  57.         If System.IO.File.Exists(FileName) = True Then
  58.             Dim file As String() = IO.File.ReadAllLines(FileName)
  59.  
  60.             For a = 0 To file.GetUpperBound(0)
  61.                 If file(a).Length > Key.Length Then
  62.                     If file(a).Substring(0, Key.Length) & ":" = Key & ":" Then Return file(a).Substring(Key.Length + 1, file(a).Length - (Key.Length + 1))
  63.                 End If
  64.             Next
  65.         End If
  66.  
  67.         Return Nothing
  68.     End Function
  69.  
  70.     Public Function KeyExists(ByVal FileName As String, ByVal Key As String) As Boolean
  71.         Dim a As Integer
  72.  
  73.         If System.IO.File.Exists(FileName) = True Then
  74.             Dim file As String() = IO.File.ReadAllLines(FileName)
  75.  
  76.             For a = 0 To file.GetUpperBound(0)
  77.                 If file(a).Length > Key.Length Then
  78.                     If file(a).Substring(0, Key.Length) & ":" = Key & ":" Then Return True
  79.                 End If
  80.             Next
  81.         End If
  82.  
  83.         Return False
  84.     End Function
  85. End Module
  86.  
  87. 'Reference excess
  88. 'dir = FileName.Substring(x, FileName.IndexOf("/", i) - x)
  89. 'file = FileName.Substring(x, FileName.Length - x)
Add Comment
Please, Sign In to add comment