Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:07:33+00:00 2026-06-14T18:07:33+00:00

I have problems when trying to delete/create/write to this file. The error: The process

  • 0

I have problems when trying to delete/create/write to this file.

The error:

The process cannot access the file 'C:\Users\Administrador\AppData\Local\Temp\PlayList_temp.txt' because it is being used by another process.

The highlighted lines by debugger:

If System.IO.File.Exists(Temp_file) = True Then System.IO.File.Delete(Temp_file)
System.IO.File.Create(Temp_file)

(Any of two, if I delete one line, the other line takes the same error id)

The sub:

Public Sub C1Button2_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If Not playerargs = Nothing Then

        Dim Str As String
        Dim Pattern As String = ControlChars.Quote
        Dim ArgsArray() As String
        Dim Temp_file As String = System.IO.Path.GetTempPath & "\PlayList_temp.txt"
        Dim objWriter As New System.IO.StreamWriter(Temp_file)

        Str = Replace(playerargs, " " & ControlChars.Quote, "")
        ArgsArray = Split(Str, Pattern)

        If System.IO.File.Exists(Temp_file) = True Then System.IO.File.Delete(Temp_file)
        System.IO.File.Create(Temp_file)

    For Each folder In ArgsArray
        If Not folder = Nothing Then
            Dim di As New IO.DirectoryInfo(folder)
            Dim files As IO.FileInfo() = di.GetFiles("*")
                Dim file As IO.FileInfo

            For Each file In files
                ' command to writleline
                'Console.WriteLine("File Name: {0}", file.Name)
                'Console.WriteLine("File Full Name: {0}", file.FullName)
                objWriter.Write(file.FullName)
                objWriter.Close()
            Next
        End If
        Next

    If randomize.Checked = True Then
            RandomiseFile(Temp_file)
    End If

    Process.Start(userSelectedPlayerFilePath, playerargs)
    If autoclose.Checked = True Then
        Me.Close()
    End If
    Else
    MessageBox.Show("You must select at least one folder...", My.Settings.APPName)
    End If
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:07:34+00:00Added an answer on June 14, 2026 at 6:07 pm

    First, File.Create creates or overwrite the file specified, so you don’t need to Delete it before.
    Second, initializing a StreamWriter as you do, blocks the file and therefore you can’t recreate it after.
    So, simply move the StreamWriter intialization after the File.Create, or better, remove altogether the File.Create and use the StreamWriter overload that overwrites the file

    ....
    If Not playerargs = Nothing Then
        ....
        Dim Temp_file As String = System.IO.Path.GetTempPath & "\PlayList_temp.txt"
    
        Using objWriter As New System.IO.StreamWriter(Temp_file, false)
            For Each folder In ArgsArray
                If Not folder = Nothing Then
                    Dim di As New IO.DirectoryInfo(folder)
                    Dim files As IO.FileInfo() = di.GetFiles("*")
                    Dim file As IO.FileInfo
                    For Each file In files
                        ' command to writleline
                        'Console.WriteLine("File Name: {0}", file.Name)
                        'Console.WriteLine("File Full Name: {0}", file.FullName)
                        objWriter.Write(file.FullName)
                        ' objWriter.Close() 
                    Next
                End If
            Next
        End Using ' Flush, close and dispose the objWriter
        ....
    

    Noticed also that you close the writer while still inside the loop, use the Using statement to close correctly the writer after the work is done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problems trying to load png file into my application. It seems to
I'm trying to create a SVN Eclipese EFS plugin and have problems when getting
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I am trying to create a csv file of some data. I have wrote
I have problems when I'm trying to do numerical differentiation in Matlab. But my
I'm trying out KO but have problems getting observableArray to work, the code is
I have problems with Darwin Streaming server 5.5.5 on Debian. When i'm trying to
I'm having some problems trying to perform a query. I have two tables, one
I have the following problems: First: I am trying to do a 32-spaces bitwise
I have been trying to learn Erlang and have been running into some problems

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.