I'm trying to write a string out to a file using File.AppendAllText, but I need to do it several times over the course of the program.

I'm not using threading, and to give the file time to close, I'm letting my program sleep for a couple of seconds after it finishes the first write.

However, I'm getting a "This process cannot access the file [my filepath] because it is being used by another process."

I don't have the file open in a text editor or anything, and the program only uses AppendAllText, so it should have closed any instances of the file that the program had open already, right?

Any help is appreciated!

We'll be able to help better if you post code. I understand being apprehensive about posting code - I do the same thing myself (that is, use lots of words and not enough code).

I always use System.IO methods when writing to files. You can create a text file with File.Create and then place it into a StreamWriter and use StreamWriter.Write to attach your string. You need to make sure to close your StreamWriter when you're done with the file though!

Well, I didn't post code because it's scattered through several subs, and this seemed more like a semantics issue than design. And it's my understanding that File.AppendAllText() does use System.IO, it just masks it.

However, below is one of my list-building subroutines. At the bottom, you'll see outPutToFile and outPutErrorToFile, which are subroutines that do a little formatting and dump my data into a file.

The line after that is where I get my error, regardless of where I place it (before my output calls, at the head of the sub, anywhere), it throws the error I was describing.

    Sub mySub(ByVal inputString As String)
        Console.WriteLine("Gexa")
        Dim inputArray As String()()

        inputArray = excelPullmySub(inputString, "")

        Dim list As New List(Of String())
        Dim errorList As New List(Of String())

        'data starts on row seven
        For i As Integer = 0 To inputArray.Length - 1
            Dim tempArray(9) As String

            If i + 7 <= inputArray.Length - 1 Then
                Try
                    tempArray(0) = "SUB"
                    tempArray(1) = "1"
                    tempArray(2) = inputArray(i + 7)(1)
                    tempArray(3) = inputArray(i + 7)(2)
                    tempArray(4) = DateTime.FromOADate(CType(inputArray(i + 7)(5), Double)).ToString
                    tempArray(5) = DateTime.FromOADate(CType(inputArray(i + 7)(6), Double)).ToString
                    tempArray(6) = inputArray(i + 7)(4)
                    tempArray(7) = inputArray(i + 7)(7)
                    tempArray(8) = inputArray(i + 7)(12)
                    tempArray(9) = "Special"

                    If String.IsNullOrEmpty(tempArray(2)) And String.IsNullOrEmpty(tempArray(3)) Then
                        Try
                            errorList.Add(tempArray)
                        Catch ex As Exception

                        End Try
                    Else
                        list.Add(tempArray)
                    End If
                Catch ex As Exception
                    Console.WriteLine(ex.ToString + " Error Occurred")
                End Try
            End If
        Next
        outPutToFile(list)
        outPutErrorToFile(errorList)
        File.AppendAllText(inputString) 'The code that throws the error

Thanks!

Isn't the correct syntax: System.IO.File.AppendAllText(filepath, strToAppend)

not: System.IO.File.AppendAllText(strToAppend)?

It is: in this case, it was a typo, though, not a cause. I apologise.

Aaaaand it turns out that my append wasn't the problem, it was an open streamreader that I hadn't accounted for. Sorry to trouble you folks.
Thanks, though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.