none
StreamWriter crashes when trying to edit a file

    Question

  • I'm trying to make code create new file and write in it.

    Code successfully compiles the string but when I declare new StreamWriter, I get System.IO.IOException

    Dim path As String = Module1.asufail
            Dim i As Integer
            Dim aryText(7) As String
    
            aryText(0) = "[Game path]"
            aryText(1) = "_"
            aryText(2) = "[Chosen nickname]"
            aryText(3) = "_"
            aryText(4) = "[Game file]"
            aryText(5) = "_"
            aryText(6) = "[Fresh config]"
            aryText(7) = "True"
    
            Dim objWriter As New System.IO.StreamWriter(path, False)
    
            For i = 0 To 7
                objWriter.WriteLine(aryText(i))
            Next
    
            objWriter.Close()

    Module1.asufail is completely working and opens the file, when I manually create it.

    Exeption comes on this line:

    Dim objWriter As New System.IO.StreamWriter(path, False)

    What am I doing wrong and how to fix it?

    Thanks in advance!

    EDIT: Forgot to mention, exception says file is being used by another program, eventhough code itself creates the file



    Sunday, September 29, 2013 8:24 PM

Answers

  • Got a fix for it! I was using

    System.IO.File.Create

    And code thought it was still running.

    Anyway it's solved now and thanks to everyone for hints and tips!

    • Marked as answer by TheCreepySheep Monday, September 30, 2013 3:41 PM
    Monday, September 30, 2013 3:40 PM

All replies

  • I don't know what you're doing in this - but no matter (and not being critical) - I suspect there's a much easier way to go about it.

    If you will, explain your goal please?


    Please call me Frank :)

    Sunday, September 29, 2013 8:47 PM
  • Hello,

    See what happens when you try this which hopefully you realize creates a new file and does not edit an existing file.

    Dim OutFile As String = IO.Path.Combine(Application.StartupPath, "Data.txt")
    Dim aryText() As String = {"[Game path]", "_", "[Chosen nickname]", "_", "[Game file]", "_", "[Fresh config]", "True"}
    
    Using Writer As New System.IO.StreamWriter(OutFile, False)
        For i As Integer = 0 To 7
            Writer.WriteLine(aryText(i))
        Next
    End Using
    
    Process.Start(OutFile)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


    Sunday, September 29, 2013 9:15 PM
  • Hello,

    See what happens when you try this which hopefully you realize creates a new file and does not edit an existing file.

    Dim OutFile As String = IO.Path.Combine(Application.StartupPath, "Data.txt")
    Dim aryText() As String = {"[Game path]", "_", "[Chosen nickname]", "_", "[Game file]", "_", "[Fresh config]", "True"}
    
    Using Writer As New System.IO.StreamWriter(OutFile, False)
        For i As Integer = 0 To 7
            Writer.WriteLine(aryText(i))
        Next
    End Using
    
    Process.Start(OutFile)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


    IOException on
    Using Writer As New System.IO.StreamWriter(OutFile, False)
    It says the file is being used by another process, even if I delete it and the code itself creates it

    Monday, September 30, 2013 3:17 PM
  • I don't know what you're doing in this - but no matter (and not being critical) - I suspect there's a much easier way to go about it.

    If you will, explain your goal please?


    Please call me Frank :)

    I want this piece of code edit a textfile
    Monday, September 30, 2013 3:18 PM
  • I don't know what you're doing in this - but no matter (and not being critical) - I suspect there's a much easier way to go about it.

    If you will, explain your goal please?


    Please call me Frank :)

    I want this piece of code edit a textfile


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim FileName As String = IO.Path.Combine(Application.StartupPath, "Data.txt")
        Dim aryText() As String = {"[Game path]", "_",
                                   "[Chosen nickname]", "_",
                                   "[Game file]", "_", "[Fresh config]", "True"}
    
        Using Writer As New System.IO.StreamWriter(FileName, False)
            For i As Integer = 0 To 7
                Writer.WriteLine(aryText(i))
            Next
        End Using
    
        Dim Lines As String() = IO.File.ReadAllLines(FileName)
        Using Writer As New System.IO.StreamWriter(FileName, False)
            For Each line In Lines
                If line.Contains("Game") Then
                    Writer.WriteLine(line.ToLower.Replace("game", "").Replace(" ", ""))
                Else
                    Writer.WriteLine(line.ToUpper)
                End If
            Next
        End Using
    
        Process.Start(FileName)
    End Sub

    Result

    [path]
    _
    [CHOSEN NICKNAME]
    _
    [file]
    _
    [FRESH CONFIG]
    TRUE


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, September 30, 2013 3:26 PM
  • Got a fix for it! I was using

    System.IO.File.Create

    And code thought it was still running.

    Anyway it's solved now and thanks to everyone for hints and tips!

    • Marked as answer by TheCreepySheep Monday, September 30, 2013 3:41 PM
    Monday, September 30, 2013 3:40 PM
  • Got a fix for it! I was using

    System.IO.File.Create

    And code thought it was still running.

    Anyway it's solved now and thanks to everyone for hints and tips!

    Which goes against "editing" a file as IO.File.Create overwrites an existing file and returns a FileStream as can be seen in a project I have on SkyDrive. And you can ensure the stream is closed immediately i.e.

    IO.File.Create("SomeFile.txt").Dispose()


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Monday, September 30, 2013 4:19 PM
  • Got a fix for it! I was using

    System.IO.File.Create

    And code thought it was still running.

    Anyway it's solved now and thanks to everyone for hints and tips!

    Sorry for the delay. I've been in meetings all morning.

    Glad you got it worked out. :)


    Please call me Frank :)

    Monday, September 30, 2013 5:31 PM
  • Got a fix for it! I was using

    System.IO.File.Create

    And code thought it was still running.

    Anyway it's solved now and thanks to everyone for hints and tips!

    Why didn't you include that code in the original post?  That would have been helpful to know and the error would have been revealed immediately!  
    Monday, September 30, 2013 8:10 PM