Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

INI Filesystem

INI Filesystem


So, here is my INI File system. I'm not going to spend ages explaining it - I'm tired.

The system allows easy file creation, edition and removal. The natives are designed to the best of my ability to be bug-free and of course, efficient. I made this so it would be easier for me to create files in my projects! :)

Functions (Click to View)

The INI VB code:
Code
Module ini

    Function iniGet(ByVal file As String, ByVal key As String) As String
  If (System.IO.File.Exists(file) = False) Then
    MsgBox("iniGet Error: Unable to open file to read")
    Return vbNullString
    Exit Function
  End If

  Dim OpenFile As New System.IO.StreamReader(file)
  Dim tmpKey As String = vbNullString
  Dim tmpData As String = vbNullString
  Dim tmpLine As String = ""

  While OpenFile.Peek <> -1
    tmpLine = OpenFile.ReadLine()
    If (strFind(tmpLine, "=") <> 0 Or strFind(tmpLine, "=") <> -1) Then
    If (tmpLine = "") Then
    MsgBox("iniGet Read Blankspace. Cannot continue.")
    OpenFile.Close()
    Return vbNullString
    Exit Function
    End If
    tmpKey = Microsoft.VisualBasic.Left(tmpLine, strFind(tmpLine, "="))
    If (tmpKey = key) Then
    Dim tmpRevLen As Integer = Microsoft.VisualBasic.Len(tmpLine) - (strFind(tmpLine, "=") + 1)
    tmpData = Microsoft.VisualBasic.Right(tmpLine, tmpRevLen)
    OpenFile.Close()
    Return tmpData
    Exit Function
    End If
    End If
  End While
  OpenFile.Close()
  Return vbNullString
    End Function

    Function iniSet(ByVal file As String, ByVal key As String, ByVal data As String)
  If (System.IO.File.Exists(file) = False) Then
    Return -1
    Exit Function
  End If

  Dim tmpKey As String
  Dim tmpData As String
  Dim tmpLine As String = ""
  Dim tmpFileContent As String

  Dim tmpFile As New System.IO.StreamReader(file)
  tmpFileContent = tmpFile.ReadToEnd()
  tmpFile.Close()

  Dim OpenFile As New System.IO.StreamReader(file)
  If (Microsoft.VisualBasic.Val(iniGet(file, key)) <> -1) Then
    While OpenFile.Peek() <> -1
    tmpLine = OpenFile.ReadLine()
    If (strFind(tmpLine, "=") <> -1) Then
    tmpKey = Microsoft.VisualBasic.Left(tmpLine, strFind(tmpLine, "="))
    If (tmpKey = key) Then
    Dim tmpRevLen As Integer = Microsoft.VisualBasic.Len(tmpLine) - (strFind(tmpLine, "=") + 1)
    tmpData = tmpKey & "=" & Microsoft.VisualBasic.Right(tmpLine, tmpRevLen)
    OpenFile.Close()
    Dim OpenFileWrite As New System.IO.StreamWriter(file)
    OpenFileWrite.Write("")
    tmpFileContent = Microsoft.VisualBasic.Strings.Replace(tmpFileContent, tmpData, key & "=" & data)
    OpenFileWrite.Write(tmpFileContent)
    OpenFileWrite.Close()
    Return 1
    Exit Function
    End If
    End If
    End While
  End If
  'Else
  OpenFile.Close()
  Dim _OpenFile As New System.IO.StreamWriter(file, True)

  _OpenFile.WriteLine(key & "=" & data)
  _OpenFile.Close()
  Return 1
  Exit Function
    End Function

    Function strFind(ByVal haystack As String, ByVal needle As String)
  Return Microsoft.VisualBasic.Strings.InStr(haystack, needle) - 1
    End Function

    Function CreateFile(ByVal file As String)
  If (System.IO.File.Exists(file)) Then
    Return 0
    Exit Function
  End If
  Dim CreatedFile As FileStream = System.IO.File.Create(file)
  CreatedFile.Close()
  Return 1
    End Function

    Function GetLineFromString(ByVal str As String)
  For I = 0 To Microsoft.VisualBasic.Len(str) - 1
    If (str(I) = Environment.NewLine) Then
    GetLineFromString = Microsoft.VisualBasic.Left(str, I)
    Exit Function
    End If
  Next
  Return -1
    End Function

    Function DeleteFile(ByVal file As String)
  If (System.IO.File.Exists(file)) Then
    System.IO.File.Delete(file)
  Else
    Return 0
    Exit Function
  End If
  Return 1
    End Function
End Module
'MCDORK - hackforums.net 2013 - DO NOT DISTRIBUTE.
Save the file to your main project folder as 'ini.vb' (or whatever you want to call it)

Hope you enjoy! If you have any questions, please post here :)
I'm not all that protective, but please make sure you keep my name at the bottom of it! :)
I’ll have what she’s having
First turn strict on. Learn data types. Learn about code reuse. Take this

Code
Function DeleteFile(ByVal file As String)
  If (System.IO.File.Exists(file)) Then
    System.IO.File.Delete(file)
  Else
    Return 0
    Exit Function
  End If
  Return 1
    End Function

Functions should have an As clause. Why are you returning 0 or 1? Where is the functions modifier? What about exception handling?

You are using loads of VB6 left overs also.
Msgbox.
Microsoft.VisualBasic.Len.
Like Ident stated, now, let's turn Option Strict on...
This method is far from being efficient.
You could just do something like that:
[Image: 3269089713e50d48e50e4d5bb760fb95.png]

It's smaller, no ?
this edit is getting worse. how is System.IO.File.Exist compatible with net? You really have no fucking clue what u copy and pasted above
System.IO ?
Compatible with .Net ?
Since when ?
An INI filesystem? 🤨... Windows started out with INI for settings and configuration, and then realized that it was horrible for larger and more complex data, which is why XML, and also the Registry exists today. So what is the reasoning behind this idea of using it as a filesystem?
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
Code
Private Declare Ansi Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
    Private Declare Ansi Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32

    Public Function INI_ReadValueFromFile(ByVal strSection As String, ByVal strKey As String, ByVal strDefault As String, ByVal strFile As String) As String
  Dim strTemp As String = Space(1024), lLength As Integer
  lLength = GetPrivateProfileString(strSection, strKey, strDefault, strTemp, strTemp.Length, strFile)
  Return (strTemp.Substring(0, lLength))
    End Function

    Public Function INI_WriteValueToFile(ByVal strSection As String, ByVal strKey As String, ByVal strValue As String, ByVal strFile As String) As Boolean
  Return (Not (WritePrivateProfileString(strSection, strKey, strValue, strFile) = 0))
    End Function

Code
INI_ReadValueFromFile("Test", "Test", "", ".\settings.ini")

Code
INI_WriteValueToFile("Test", "Test", "A text", ".\settings.ini")


Possibly Related Threads...
Thread / Author Replies Views Last Post
by TheArchitectDec 31, 2022 02:38 AM 12 302 Dec 31, 2022 02:38 AM
Last Post: TheArchitect
[C#] by SIM-4785719Jul 27, 2020 06:40 PM 6 214 Jul 27, 2020 06:40 PM
Last Post: gigajew
[.NET] by TostigMay 31, 2019 10:31 AM 4 381 May 31, 2019 10:31 AM
Last Post: Alpha_Coder