Visual Basic Sequential File IO
-
First off, I have found some articles on the MSDN website that have pointed me in the right direction but don't have all the answers. Also, I am using Visual Basic 6.0. I need to be able to access a file and do a couple of things with it: 1) I need to find out if it exists and if not, then create it. I found System.IO.File.Exists("c:\ExistingFileName") but I get an error. I believe this error is due to the fact that I have not told the compiler I am using the system namespace. How do you signal the compiler you are using the System namespace in Basic? In C it would be using System.IO.File, but I have no clue in Basic.:confused: 2) What is the command to create a sequential file in Basic and what variable will I have access to? 3) How do I add to it/ close it when I am done? Thank you, Zach Calvert ----------------- http://www.zachcalvert.com
-
First off, I have found some articles on the MSDN website that have pointed me in the right direction but don't have all the answers. Also, I am using Visual Basic 6.0. I need to be able to access a file and do a couple of things with it: 1) I need to find out if it exists and if not, then create it. I found System.IO.File.Exists("c:\ExistingFileName") but I get an error. I believe this error is due to the fact that I have not told the compiler I am using the system namespace. How do you signal the compiler you are using the System namespace in Basic? In C it would be using System.IO.File, but I have no clue in Basic.:confused: 2) What is the command to create a sequential file in Basic and what variable will I have access to? 3) How do I add to it/ close it when I am done? Thank you, Zach Calvert ----------------- http://www.zachcalvert.com
-
it doesn't use the system namespace. I am using this for a school project and I must use the system namespace. Also, it only reports error codes. I need to be able to check to see if it exists, not catch errors. ----------------- http://www.zachcalvert.com
-
it doesn't use the system namespace. I am using this for a school project and I must use the system namespace. Also, it only reports error codes. I need to be able to check to see if it exists, not catch errors. ----------------- http://www.zachcalvert.com
The
System.IO
namespace contains theFile
class. This class contains the shared.Exists
method you use to check to see if the filename exists, prefferable using the FULL PATH to the file..Exists
isn't documented to throw any exceptions. It just returnsTrue
orFalse
. In a normal installation of Visual Studio .NET or Visual Basic.NET, theSystem
namespace is automatically imported into your project. You don't have to use theImports System
statement at the top of your code because it's already done behind the scenes. You DO, on the other hand, have to put in theImports System.IO
yourself, again, at the top of your code.Imports System.IO
Public Class Form1
.
.
.
Private Sub WhatEver()
' You don't nedd to specify the System.IO namespace prefix because you
' already Imported it at the top of your code.
If File.Exists("C:\myTestFile.txt") Then
.
.
.RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
The
System.IO
namespace contains theFile
class. This class contains the shared.Exists
method you use to check to see if the filename exists, prefferable using the FULL PATH to the file..Exists
isn't documented to throw any exceptions. It just returnsTrue
orFalse
. In a normal installation of Visual Studio .NET or Visual Basic.NET, theSystem
namespace is automatically imported into your project. You don't have to use theImports System
statement at the top of your code because it's already done behind the scenes. You DO, on the other hand, have to put in theImports System.IO
yourself, again, at the top of your code.Imports System.IO
Public Class Form1
.
.
.
Private Sub WhatEver()
' You don't nedd to specify the System.IO namespace prefix because you
' already Imported it at the top of your code.
If File.Exists("C:\myTestFile.txt") Then
.
.
.RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
I'm using VB 6.0, and it doesn't let me do Imports System.IO What do I do now? ----------------- http://www.zachcalvert.com
-
I'm using VB 6.0, and it doesn't let me do Imports System.IO What do I do now? ----------------- http://www.zachcalvert.com
OK. Then throw out EVERYTHING about namespaces and
System
,System.IO
,File.Exists
... EVERYTHING. VB.NET is a VERY different language from VB6 and none of this stuff applies to VB6. Now, you'll have to use theFileSystemObject
to see if the specified file exists. This will return eitherTrue
orFalse
, it doesn't throw any errors.Set fs = CreateObject("Scripting.FileSystemObject") If fs.FileExists("C:\\myTestFile.txt") Then . . ' The file exists... . Else . . ' The file doesn't exist... . End If
To open a text file for sequential write, you'll use the
Open
statement (docs here[^]):Open "C:\\myTestFile.txt" For Output As #1 Print #1, "This is line 1" Print #1, "This is line 2..." Print #1, "This is line 3..." Close #1
RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome