PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Exist File or Not-VBForums
Results 1 to 18 of 18

Thread: Exist File or Not

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Exist File or Not

    How to make when they start the program if they dont have the file (config.ini) the program auto to create it , and how to make what to be put in the config.ini

    like:
    Config.ini:
    // This is program for...
    // site and other stuff

    [Path]
    GameServer1 Path =
    GameServer2 Path =
    GameServer3 Path =

    [File]
    Name1 = (name of the .exe file that the program will open, they will add the stuff after =)
    Name2 =
    Name3 =

    [Port]
    Port1 =
    Port2 =
    Port3 =

    [Custom]
    AutoRestart = 1 (1-Enabled,0-Disabled) *(here to has this hole line with enabled or disabled)
    Time = (here to put number in seconds, after what time if the .exe closes to run the .exe file again)

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

    Re: Exist File or Not

    Check out user settings rather than using an ini file. Here's a link to help get you started.
    http://msdn.microsoft.com/en-us/libr...11(VS.80).aspx
    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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Re: Exist File or Not

    but i want to make it with .ini couse people will need it to change it manual with theyr settings in .ini file
    they will need to put the path to the file like : C:\file\
    File = Game.exe
    Port = what will add after the game.exe (like: 127.0.0.1 55970 127.0.0.1 55901)

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Exist File or Not

    This will get you started. You should be able to take it from here.
    vb.net Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         If Not System.IO.File.Exists("c:\config.ini") Then
    3.             Dim TextFile As New System.IO.StreamWriter("c:\config.ini")
    4.             TextFile.WriteLine("[Path]")
    5.             'etc
    6.             TextFile.Close()
    7.         End If
    8.     End Sub

  5. #5
    Addicted Member vb_ftw's Avatar
    Join Date
    Dec 2010
    Posts
    139

    Re: Exist File or Not

    what hack suggested and from there when your program starts u can read the text file into an array and then make changes, restore settings etc etc and then rewrite the ini file using the array...

  6. #6
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    864

    Re: Exist File or Not

    Or use nice classess to handle the inifiles...
    Code:
    Imports System.IO
    Public Class Ini
    
        Private _Sections As New Dictionary(Of String, Dictionary(Of String, String))
        Private _FileName As String
        ''' <summary>
        ''' </summary>
        ''' <param name="IniFileName">Drive,Path and Filname for the inifile</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal IniFileName As String)
    
            Dim Rd As StreamReader
            Dim Content As String
            Dim Lines() As String
            Dim Line As String
            Dim Key As String
            Dim Value As String
            Dim SectionValues As Dictionary(Of String, String) = Nothing
            Dim Name As String
    
            _FileName = IniFileName
    
            'check if the file exists
            If Not File.Exists(IniFileName) Then
                Throw New FileLoadException(String.Format("The file {0} is not found", IniFileName))
            Else
                'Read the file if present.
                Rd = New StreamReader(IniFileName)
                Content = Rd.ReadToEnd
                'Split It into lines
                Lines = Content.Split(vbCrLf)
    
                'Place the content in an object sructure
                For Each Line In Lines
    
                    'Trim the line
                    Line = Line.Trim
                    If Line.Length <= 2 OrElse Line.Substring(0, 1) = "'" OrElse Line.Substring(0, 3).ToUpper = "REM" Then
                        'There's no valid data or it's commented out... Do nothing 
                    ElseIf Line.IndexOf("[") = 0 AndAlso Line.IndexOf("]") = Line.Length - 1 Then
                        'We hit a section
                        Name = Line.Replace("]", String.Empty).Replace("[", String.Empty).Trim.ToUpper
                        SectionValues = New Dictionary(Of String, String)
                        _Sections.Add(Name.ToUpper, SectionValues)
    
                        'An = character as the firstcharacter is an invalid line... let's be relaxed an just ignore it.
                    ElseIf Line.IndexOf("=") > 0 AndAlso SectionValues IsNot Nothing Then
                        'We hit a value line , empty line or out commented line
                        'we don't use split as that character could be part of the value as well
                        Key = Line.Substring(0, Line.IndexOf("=")).Trim
                        If Line.IndexOf("=") = Line.Length - 1 Then
                            Value = String.Empty
                        Else
                            Value = Line.Substring(Line.IndexOf("=") + 1, Line.Length - (Line.IndexOf("=") + 1)).Trim
                        End If
                        'Add the valu to 
                        SectionValues.Add(Key.ToUpper, Value)
                    End If
                Next
    
                Rd.Close()
                Rd.Dispose()
                Rd = Nothing
    
            End If
        End Sub
    
        Public Function GetValue(ByVal Section As String, ByVal Name As String) As String
    
            If _Sections.ContainsKey(Section.ToUpper) Then
                Dim SectionValues As Dictionary(Of String, String) = Nothing
                SectionValues = _Sections(Section.ToUpper)
                If SectionValues.ContainsKey(Name.ToUpper) Then
                    Return SectionValues(Name.ToUpper)
                End If
            End If
    
            Return Nothing 'if preferred return String.empty here
    
        End Function
    
        Public Function SetValue(ByVal Section As String, ByVal Name As String, ByVal Value As String, Optional ByVal Save As Boolean = False) As Boolean
            Dim SectionValues As Dictionary(Of String, String) = Nothing
            Name = Name.ToUpper.Trim
            Section = Section.ToUpper.Trim
            If _Sections.ContainsKey(Section) Then
                SectionValues = _Sections(Section)
                If SectionValues.ContainsKey(Name) Then
                    SectionValues.Remove(Name)
                End If
                SectionValues.Add(Name, Value)
            Else
                SectionValues = New Dictionary(Of String, String)
                _Sections.Add(Section, SectionValues)
                SectionValues.Add(Name, Value)
            End If
    
            If Save Then
                Return SaveIniFile()
            Else
                Return True
            End If
    
        End Function
    
        ''' <summary>
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks>Saving the inifile will remove all comments and invalid lines and all sections and valuenames will be in capitals.</remarks>
        Public Function SaveIniFile() As Boolean
    
            Dim Rw As StreamWriter
            Dim SectionPair As KeyValuePair(Of String, Dictionary(Of String, String))
            Dim ValuePair As KeyValuePair(Of String, String)
    
            Dim Pth As String = Path.GetDirectoryName(_FileName)
    
            If Directory.Exists(Pth) Then
                Rw = New StreamWriter(_FileName, False)
                For Each SectionPair In _Sections
                    Rw.WriteLine("[" & SectionPair.Key & "]")
                    If SectionPair.Value IsNot Nothing Then
                        For Each ValuePair In SectionPair.Value
                            Rw.WriteLine(ValuePair.Key & "=" & ValuePair.Value)
                        Next
                    End If
                Next
                Rw.WriteLine("")
                Rw.Flush()
                Rw.Close()
                Rw.Dispose()
                Rw = Nothing
                SaveIniFile = True
            End If
    
        End Function
    
        Function DeleteValue(ByVal Section As String, ByVal Name As String, Optional ByVal Save As Boolean = False) As Boolean
    
            Dim SectionValues As Dictionary(Of String, String) = Nothing
    
            Name = Name.ToUpper.Trim
            Section = Section.ToUpper.Trim
            If _Sections.ContainsKey(Section) Then
                SectionValues = _Sections(Section)
                If SectionValues.ContainsKey(Name) Then
                    SectionValues.Remove(Name)
                End If
            End If
    
            If Save Then
                Return SaveIniFile()
            Else
                Return True
            End If
    
        End Function
    
        Function DeleteSection(ByVal Section As String, Optional ByVal Save As Boolean = False) As Boolean
    
            Dim SectionValues As Dictionary(Of String, String) = Nothing
    
            Section = Section.ToUpper.Trim
            If _Sections.ContainsKey(Section) Then
                _Sections.Remove(Section)
            End If
    
            If Save Then
                Return SaveIniFile()
            Else
                Return True
            End If
    
        End Function
    
    
    End Class
    Usage:
    Code:
            Dim Fl As String = "DRIVENAME:\FOLDERNAME\FILENAME.ini"
            Dim My_Ini As New Ini(Fl)
    
            My_Ini.SetValue("NewSection", "Testvalue", "230", True)
    
            MsgBox(My_Ini.GetValue("NewSection", "TestValue"))
    
            My_Ini.DeleteValue("NewSection", "TestValue", True)
            My_Ini.DeleteSection("NewSection", True)
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,778

    Re: Exist File or Not

    Quote Originally Posted by diablo21 View Post
    but i want to make it with .ini couse people will need it to change it manual with theyr settings in .ini file
    they will need to put the path to the file like : C:\file\
    File = Game.exe
    Port = what will add after the game.exe (like: 127.0.0.1 55970 127.0.0.1 55901)
    That's no reason to use an INI file over user/application settings. .NET settings are stored in either the primary config file or the user config file. They are both XML files, easily accessible and specifically designed to hand-editable.

  8. #8
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    864

    Re: Exist File or Not

    There are some downsides to using settings as far as I know.
    - not use diffrent settings sets and decide at runtime which one you want to use or switch between them at runtime.
    - not organize settings into sections (less structure)
    - not decide where the settings are stored.
    - not keep the settings if the application is de-installed by the setup wizzard. So they will remain in a new installation

    But I have to admid I never use .Ini Files other then legacy reasons or to read them for software conversions.
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

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

    Re: Exist File or Not

    not organize settings into sections (less structure)
    I thought you could specify any serializable type as a setting, to me that would be more structure.

    not use diffrent settings sets and decide at runtime which one you want to use or switch between them at runtime.
    I think you could do this with an array or collection of a class.

    not decide where the settings are stored.
    On the other hand, you don't have to worry about file system access. If I had to choose, I would pick the latter.

    not keep the settings if the application is de-installed by the setup wizzard. So they will remain in a new installation
    I don't believe a user would want your files hanging out when they uninstall your application, so I don't see this as a problem.

    I've never ran into a situation where I needed an ini file rather than taking advantage of what we get with settings, so I would take a hard look at what you are doing before implementing an ini file.
    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

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Re: Exist File or Not

    okey i use this basic step:
    Code:
    If Not System.IO.File.Exists("config.ini") Then
                Dim TextFile As New System.IO.StreamWriter("config.ini")
               TextFile.WriteLine("[Path]")
                'etc
                TextFile.Close()
            End If
    but how to make it, if file exist (config.ini) then to refresh the information inside the file
    couse now only make if exist nothing doing, if dont exist create it with the information , how should be fixed?

  11. #11
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    864

    Re: Exist File or Not

    Like this:

    Code:
      
            Dim Fs As FileStream
            Dim RW As StreamWriter
    
            If System.IO.Directory.Exists(Path) AndAlso Not System.IO.File.Exists(Path & FileName) Then
    
                Fs = File.Create(Path & FileName)
                RW = New StreamWriter(Fs)
    
            Else
                'Do something
            End If
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Re: Exist File or Not

    is there a some kind of reload the file couse this code for me not work that you give me

  13. #13
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    864

    Re: Exist File or Not

    I've extended it a bit for you,

    Code:
     Dim Path As String = "C:\Test\"
            Dim FileName As String = "Test.ini"
            Dim Fs As FileStream
            Dim RW As StreamWriter
    
            If System.IO.Directory.Exists(Path) AndAlso Not System.IO.File.Exists(Path & FileName) Then
    
                Fs = File.Create(Path & FileName)
                RW = New StreamWriter(Fs)
                RW.WriteLine("[PATH]")
                'do the rest
    
                RW.Flush()
                RW.Close()
                RW.Dispose()
                RW = Nothing
            Else
                'Do something
            End If
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Re: Exist File or Not

    i got underline on streamwriter filestream and File.Create(Path * filename)

  15. #15
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    864

    Re: Exist File or Not

    Put this on top of your form code
    Imports System.IO
    why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
    for every question you ask provide an answer on another thread.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Re: Exist File or Not

    okey now its fixed what to put on :
    Else
    'Do something
    and how i can make space between the [Path] and [File]
    couse its showing like
    [Path]
    [File]
    to be like
    [Path]
    here the text

    [File]

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Re: Exist File or Not

    this script dont work dont reload the code from the program into the .ini
    anyway lets leave it only the program to make the file is enough..

    only if you can tell me how to make the space between the [Path] and [File] like i write little before

  18. #18
    New Member
    Join Date
    Dec 2014
    Posts
    2

    Re: Exist File or Not

    Quote Originally Posted by wild_bill View Post
    I thought you could specify any serializable type as a setting, to me that would be more structure.


    I think you could do this with an array or collection of a class.


    On the other hand, you don't have to worry about file system access. If I had to choose, I would pick the latter.


    I don't believe a user would want your files hanging out when they uninstall your application, so I don't see this as a problem.

    I've never ran into a situation where I needed an ini file rather than taking advantage of what we get with settings, so I would take a hard look at what you are doing before implementing an ini file.

    One big disadvantage in using settings file is if your program needs to have an ability to export to an external file to share with another person. Like if you are building a character for a game and saving the build in a program. You can't just export your settings.settings file for someone else to load into their system or profile. Since the xml file is built into a randomly generated folder in the user profile folder.

    To clarify:

    Program stores variables (Health, Defense, Critical Attack Chance Percentage, etc, etc, etc, and character name, role, etc.)
    Settings.settings saves them brilliantly, and I am currently using this with a settings.settings, settings1.settings (for dual role characters to have stats saved in one mode and then different for another mode or role).

    However, players can have more than several characters each with dual roles. If I want to share my character to another person, I need to find the user.config in a random folder somehow, then send that file to my friend and they need to somehow load that file into the program.

    Currently no way to tell application settings to load an external file, the only way I see is to recreate the variables you have made in settings (double work), and then read / write to an external file that loads those variables and then have those variables interact with the settings.settings file. (triple work).

    Whereas if I had just set it up to write to an external file in the first place, I only have to manhandle the variables once instead of three times.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width