Results 1 to 5 of 5

Thread: [RESOLVED] InWhy!

  1. #1

    Thread Starter
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Resolved [RESOLVED] InWhy!

    ok so sql lite version of this was easy, why now do i have to make an ini version ... because boss said so.
    here it is.
    I am using a simple ini class to read and write to inis
    mostly no problem except one thing
    I need to be able to read all keys from a section without knowing exactly how many there are and grab the every variable from every key.

    below is class i'm using at moment because it is small and very litl overhead,
    I dont need a major overhaul, just wondering if anyone might can add to what i have to do what i need.

    for futher thought, grab section --> grab every key and it's variable --> use each variable in a loop
    for instance say i store a bunch of exe paths to a bunch of keys (1-100) and i want to run a loop Process.Start(eachKey'sVariable) ... how?

    Class Code:
    1. Public Class IniFile
    2.  
    3.     ' API functions
    4.     Private Declare Ansi Function GetPrivateProfileString _
    5.       Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
    6.       (ByVal lpApplicationName As String, _
    7.       ByVal lpKeyName As String, ByVal lpDefault As String, _
    8.       ByVal lpReturnedString As System.Text.StringBuilder, _
    9.       ByVal nSize As Integer, ByVal lpFileName As String) _
    10.       As Integer
    11.     Private Declare Ansi Function WritePrivateProfileString _
    12.       Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
    13.       (ByVal lpApplicationName As String, _
    14.       ByVal lpKeyName As String, ByVal lpString As String, _
    15.       ByVal lpFileName As String) As Integer
    16.     Private Declare Ansi Function GetPrivateProfileInt _
    17.       Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
    18.       (ByVal lpApplicationName As String, _
    19.       ByVal lpKeyName As String, ByVal nDefault As Integer, _
    20.       ByVal lpFileName As String) As Integer
    21.     Private Declare Ansi Function FlushPrivateProfileString _
    22.       Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
    23.       (ByVal lpApplicationName As Integer, _
    24.       ByVal lpKeyName As Integer, ByVal lpString As Integer, _
    25.       ByVal lpFileName As String) As Integer
    26.     Dim strFilename As String
    27.  
    28.     ' Constructor, accepting a filename
    29.     Public Sub New(ByVal Filename As String)
    30.         strFilename = Filename
    31.     End Sub
    32.  
    33.     ' Read-only filename property
    34.     ReadOnly Property FileName() As String
    35.         Get
    36.             Return strFilename
    37.         End Get
    38.     End Property
    39.  
    40.     Public Function GetString(ByVal Section As String, _
    41.       ByVal Key As String, ByVal [Default] As String) As String
    42.         ' Returns a string from your INI file
    43.         Dim intCharCount As Integer
    44.         Dim objResult As New System.Text.StringBuilder(256)
    45.         intCharCount = GetPrivateProfileString(Section, Key, _
    46.            [Default], objResult, objResult.Capacity, strFilename)
    47.         If intCharCount > 0 Then GetString = _
    48.            Left(objResult.ToString, intCharCount)
    49.     End Function
    50.  
    51.     Public Function GetInteger(ByVal Section As String, _
    52.       ByVal Key As String, ByVal [Default] As Integer) As Integer
    53.         ' Returns an integer from your INI file
    54.         Return GetPrivateProfileInt(Section, Key, _
    55.            [Default], strFilename)
    56.     End Function
    57.  
    58.     Public Function GetBoolean(ByVal Section As String, _
    59.       ByVal Key As String, ByVal [Default] As Boolean) As Boolean
    60.         ' Returns a boolean from your INI file
    61.         Return (GetPrivateProfileInt(Section, Key, _
    62.            CInt([Default]), strFilename) = 1)
    63.     End Function
    64.  
    65.     Public Sub WriteString(ByVal Section As String, _
    66.       ByVal Key As String, ByVal Value As String)
    67.         ' Writes a string to your INI file
    68.         WritePrivateProfileString(Section, Key, Value, strFilename)
    69.         Flush()
    70.     End Sub
    71.  
    72.     Public Sub WriteInteger(ByVal Section As String, _
    73.       ByVal Key As String, ByVal Value As Integer)
    74.         ' Writes an integer to your INI file
    75.         WriteString(Section, Key, CStr(Value))
    76.         Flush()
    77.     End Sub
    78.  
    79.     Public Sub WriteBoolean(ByVal Section As String, _
    80.       ByVal Key As String, ByVal Value As Boolean)
    81.         ' Writes a boolean to your INI file
    82.         WriteString(Section, Key, CStr(CInt(Value)))
    83.         Flush()
    84.     End Sub
    85.  
    86.     Private Sub Flush()
    87.         ' Stores all the cached changes to your INI file
    88.         FlushPrivateProfileString(0, 0, 0, strFilename)
    89.     End Sub
    90.  
    91. End Class

  2. #2

    Thread Starter
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Re: InWhy!

    current in form code
    (keep in mind i've been trying a hundred different things and it prolly doesnt work right now, just simply copying it over)

    Private Sub tmrProgWatch01_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrProgWatch01.Tick
    Dim filName As String = "FileName"
    Dim filExe As String = "FileExtension"
    Dim strName As String = objIniFile.GetString(filName, "", "")
    Dim strExe As String = objIniFile.GetString(filExe, "", "")
    If ProcessesRunning(strName) >= 1 Then
    lblInfo.Text = "There is " & vbCrLf & " copy of " & strName & vbCrLf & "program running"
    Else
    lblInfo.Text = "not running"
    'Process.Start(strExe)
    End If
    While ProcessesRunning(strName) >= 2
    For Each proc As Process In Process.GetProcessesByName(strName)
    proc.CloseMainWindow()
    Threading.Thread.Sleep(50)
    Next
    End While

    End Sub

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: InWhy!

    Huh? You are using all those API calls just to write some text to an INI file??

    If you HAVE to use an INI file then you might want to look at a .NET class like this one http://www.codeproject.com/KB/files/...ssIniFile.aspx
    However, Microsoft do recommend that you use XML files rather than INI files these days... and as such, they are fully supported by the .NET framework
    Last edited by chris128; Aug 6th, 2009 at 05:00 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: InWhy!

    To add to Chris' post: Create an app.config file, in the config file add something like this after the configuration element:

    Code:
    	<configSections>
    		<section name="FolderList" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
     </configSections>
    	<FolderList>
    		 <add key="key1" value="value1" />
    		 <add key="key2" value="value2" />
    	</FolderList>
    Then to access the name value collection use
    Code:
            Dim folderList As Specialized.NameValueCollection = CType(System.Configuration.ConfigurationSettings.GetConfig("FolderList"), Specialized.NameValueCollection)
            For Each key As String In folderList.Keys
                Debug.WriteLine(key & " - " & folderList(key))
            Next
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  5. #5

    Thread Starter
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Re: InWhy!

    working great that way, thank you
    and the api's were just in that class i borrowed, not like i write to ini's with .net to often ... in fact never, just simply a request of the overhead, but they are sattisfied with the sql lite version and the appconfig version now, so this headache is over, thanks for the reference.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width