Can New StreamReader create a file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Just Me

    Can New StreamReader create a file?

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

    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




  • Cor Ligthert

    #2
    Re: Can New StreamReader create a file?

    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.x ml")
    If IO.File.Exists( "C:\Herfried2.x ml") 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(li ne)
    line = sr.ReadLine
    Loop
    If temppath <> Nothing Then
    IO.File.Delete( temppath)
    End If
    End Sub
    End Class
    ///
    I hope this helps

    Cor


    Comment

    • Cor Ligthert

      #3
      Re: Can New StreamReader create a file?

      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


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Can New StreamReader create a file?

        " Just Me" <groups@a-znet.com> schrieb:[color=blue]
        >I need to get a StreamReader
        > Dim lsr0 As New StreamReader(Fu llPath)
        >
        > If the file does not exist I'd like it to create one.[/color]

        'StreamReader' cannot create a file.
        [color=blue]
        > Do I have to Try/Catch or is there a clean way of doing it?[/color]

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

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • Just Me

          #5
          Re: Can New StreamReader create a file?

          Thanks to both for the info


          " Just Me" <groups@a-znet.com> wrote in message
          news:eETHMjeEFH A.1932@TK2MSFTN GP14.phx.gbl...[color=blue]
          >I need to get a StreamReader
          > Dim lsr0 As New StreamReader(Fu llPath)
          >
          > 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
          >
          >
          >
          >[/color]


          Comment

          Working...