PDA

Click to See Complete Forum and Search --> : opening a file


deoblo1
Sep 1st, 2002, 02:34 AM
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


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.


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

Edneeis
Sep 1st, 2002, 03:09 AM
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):

Dim fs As System.IO.FileStream = New System.IO.FileStream(System.IO.Path.Combine(Application.StartupPath, "Main Config.ini"), IO.FileMode.OpenOrCreate)
Dim sr As New System.IO.StreamReader(fs)
Dim contents As String = sr.ReadToEnd
sr.Close()
fs.Close()
sr = Nothing
fs = Nothing


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


'At the top
Imports System.IO 'works kind of like a giant With..End WIth

Dim fs As FileStream = New FileStream(Path.Combine(Application.StartupPath, "Main Config.ini"), FileMode.OpenOrCreate)'Opens file
Dim sr As New StreamReader(fs)'here is an object to read it
Dim contents As String = sr.ReadToEnd'reads the whole file to the string
sr.Close()' clean up
fs.Close()
sr = Nothing
fs = Nothing