Results 1 to 2 of 2

Thread: opening a file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    313

    opening a file

    Well I am used to VB6 or something either that or i forgot how to program in Visual Basic. I have a modual named mdlDataInput and in there I have a public function named Main_Config(). Now in VB 6 I remember I could just go

    Code:
    open app.path + "\Main Config.ini" for input as #1
    and it would open the file just fine. Now it says some crap about a namespace and what not so i added this and it still does't work, it also gives me some problems about the app command. Can someone help me out.

    Code:
    Module mdlDataInput
        Public Function Main_Config()
            Dim open As Microsoft.VisualBasic
            ' OPEN THE MAIN_CONFIG.INI FOR INPUT
            open app.path + "\Main Config.ini" for input as #1
    
        End Function
    End Module

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You should try searching in the .NET help for 'Open File' or 'Visual Basic 6 differences' and it will give you an example. As for the App.path there is no App object in .NET use: Application.StartUpPath instead.

    Although I would say ditch the INI file and use XML instead, you can load it into a dataset and make things much easier.

    Here is a sample of how to read a file just in case (Although the xml way is better):
    VB Code:
    1. Dim fs As System.IO.FileStream = New System.IO.FileStream(System.IO.Path.Combine(Application.StartupPath, "Main Config.ini"), IO.FileMode.OpenOrCreate)
    2.         Dim sr As New System.IO.StreamReader(fs)
    3.         Dim contents As String = sr.ReadToEnd
    4.         sr.Close()
    5.         fs.Close()
    6.         sr = Nothing
    7.         fs = Nothing

    You could also shorten this if you put some Imports at the very top outside of the class:

    VB Code:
    1. 'At the top
    2. Imports System.IO 'works kind of like a giant With..End WIth
    3.  
    4.         Dim fs As FileStream = New FileStream(Path.Combine(Application.StartupPath, "Main Config.ini"), FileMode.OpenOrCreate)'Opens file
    5.         Dim sr As New StreamReader(fs)'here is an object to read it
    6.         Dim contents As String = sr.ReadToEnd'reads the whole file to the string
    7.         sr.Close()' clean up
    8.         fs.Close()
    9.         sr = Nothing
    10.         fs = Nothing
    Last edited by Edneeis; Sep 1st, 2002 at 03:15 AM.

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