• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

create, fill & close text file with comma delimited informatin

in VB.net I want to open a file and write comma delimited records to the file and close the file
can somebody please give me the code to do this ?
Cheers
0
CiaranG
Asked:
CiaranG
  • 3
  • 2
  • 2
  • +2
1 Solution
 
appariCommented:
try like this

dim csvString as string
csvstring="data1,data2,data3"
my.Computer.FileSystem.WriteAllText "c:\filename.csv", csvString ,True
0
 
sam_norianCommented:
Dim strData as String

strData= "Data,Data,Data,Data"

FileOpen(1, "C:\test.csv", OpenMode.Append)
PrintLine(1, strdata)
FileClose(1)
0
 
CiaranGAuthor Commented:
sam do I need to include anything as that I can use this method?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sam_norianCommented:
No, should just work exactly as it is above.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Using newer .Net classes:

    Dim sw As New System.IO.StreamWriter("c:\someFile.txt")
    sw.WriteLine("some,comma,delimited,data")
    sw.WriteLine("whereever,it,comes,from")
    sw.Close()
0
 
icrCommented:
You should use a "Using" statement if you are using StreamWriter. If you don't, and an exception is thrown during the sw.WriteLine (For whatever reason. Unlikely but possible in this situation. But its best to get into good habbits) the StreamWriter will not be closed. By utilising a "Using" statement, if the code execution jumps out of the "Using" block for whatever reason (whether that be because the end of the block is reached or because an exception was thrown) the Close method will be called. That way you can have a safer assurance that the handle is closed properly.

Adapting Idle_Minds code:
Using sw As New System.IO.StreamWriter("c:\someFile.txt")
    sw.WriteLine("some,comma,delimited,data")
    sw.WriteLine("whereever,it,comes,from")
End Using
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can also use an enclosing Try/Catch block, just depends on your needs...
0
 
icrCommented:
A Try/Catch/Finally block tends to be overkill and leaves the variable in scope when it is clearly of no use.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Possibly...it depends on what threw the Exception now doesn't it?  There may be other operations that you want enclosed in the same Try/Catch that could leave the StreamWriter in a state that would still allow writing to the file.

We can't claim to know all the possibile requirements of any app that will ever be written...  

Additionally, the "Using" keyword is not availale in all version of VB.Net.

Microsoft even discusses it here:
http://msdn2.microsoft.com/en-us/library/htd05whh(VS.80).aspx

    "Structured Exception Handling Instead of a Using Block - If you need finer control over the acquisition of the resources, or you need additional code in the Finally block, you can rewrite the Using block as a Try...Finally construction."
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now