We help IT Professionals succeed at work.

create, fill & close text file with comma delimited informatin

Medium Priority
251 Views
Last Modified: 2008-02-01
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
Comment
Watch Question

Commented:
try like this

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

strData= "Data,Data,Data,Data"

FileOpen(1, "C:\test.csv", OpenMode.Append)
PrintLine(1, strdata)
FileClose(1)

Author

Commented:
sam do I need to include anything as that I can use this method?
No, should just work exactly as it is above.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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()
icr

Commented:
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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
You can also use an enclosing Try/Catch block, just depends on your needs...
icr

Commented:
A Try/Catch/Finally block tends to be overkill and leaves the variable in scope when it is clearly of no use.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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."
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.