Can New StreamReader create a file?

  • Thread starter Thread starter Just Me
  • Start date Start date
J

Just Me

I need to get a StreamReader
Dim lsr0 As New StreamReader(FullPath)

If the file does not exist I'd like it to create one.

Do I have to Try/Catch or is there a clean way of doing it?





Thanks
 
Just Me,

You do not have to look if it exist to create, however I would see if it
exist to save it in between.

Very simple made without any checking for exeception something as this.

\\\
Public Class Test
Public Shared Sub main()
Dim line As String
Dim temppath As String
Dim sr As New IO.StreamReader("C:\Herfried.xml")
If IO.File.Exists("C:\Herfried2.xml") Then
temppath = System.IO.Path.GetTempFileName
IO.File.Delete(temppath)
IO.File.Move("C:\Herfried2.xml", temppath)
End If
Dim sw As New IO.StreamWriter("C:\Herfried2.xml")
line = sr.ReadLine()
Do Until line Is Nothing
sw.WriteLine(line)
line = sr.ReadLine
Loop
If temppath <> Nothing Then
IO.File.Delete(temppath)
End If
End Sub
End Class
///
I hope this helps

Cor
 
Just me,

I misreaded it, however I think that I gave you every ingredient in my
previous message to do it yourself.

When not reply

Cor
 
Just Me said:
I need to get a StreamReader
Dim lsr0 As New StreamReader(FullPath)

If the file does not exist I'd like it to create one.

'StreamReader' cannot create a file.
Do I have to Try/Catch or is there a clean way of doing it?

Check if the file exists using 'File.Exists' (namespace 'System.IO'). You
can create a file using 'File.Create'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top