Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Eleкtro en 22 Noviembre 2012, 11:30 am



Título: (solucionado) Problemas de acceso en un archivo de texto
Publicado por: Eleкtro en 22 Noviembre 2012, 11:30 am
Tengo problemas al intentar escribir datos en un archivo de texto...

Cualquier ayuda, se agradece!


Las líneas del problema:
Código
  1.            If System.IO.File.Exists(Temp_file) = True Then System.IO.File.Delete(Temp_file)
  2.            System.IO.File.Create(Temp_file)
( Cualquiera de las dos líneas, si elimino una, me da el mismo error en la otra, pero el archivo si que me lo llega a crear (vacío) )

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

El sub:
Código
  1.  Public Sub C1Button2_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.  
  3.        If Not playerargs = Nothing Then
  4.  
  5.            Dim Str As String
  6.            Dim Pattern As String = ControlChars.Quote
  7.            Dim ArgsArray() As String
  8.            Dim Temp_file As String = System.IO.Path.GetTempPath & "\PlayList_temp.txt"
  9.            Dim objWriter As New System.IO.StreamWriter(Temp_file)
  10.  
  11.            Str = Replace(playerargs, " " & ControlChars.Quote, "")
  12.            ArgsArray = Split(Str, Pattern)
  13.  
  14.            If System.IO.File.Exists(Temp_file) = True Then System.IO.File.Delete(Temp_file)
  15.            System.IO.File.Create(Temp_file)
  16.  
  17.        For Each folder In ArgsArray
  18.            If Not folder = Nothing Then
  19.                Dim di As New IO.DirectoryInfo(folder)
  20.                Dim files As IO.FileInfo() = di.GetFiles("*")
  21.                    Dim file As IO.FileInfo
  22.  
  23.                For Each file In files
  24.                    ' command to writleline
  25.                    'Console.WriteLine("File Name: {0}", file.Name)
  26.                    'Console.WriteLine("File Full Name: {0}", file.FullName)
  27.                    objWriter.Write(file.FullName)
  28.                    objWriter.Close()
  29.                Next
  30.            End If
  31.            Next
  32.  
  33.        If randomize.Checked = True Then
  34.                RandomiseFile(Temp_file)
  35.        End If
  36.  
  37.        Process.Start(userSelectedPlayerFilePath, playerargs)
  38.        If autoclose.Checked = True Then
  39.            Me.Close()
  40.        End If
  41.        Else
  42.        MessageBox.Show("You must select at least one folder...", My.Settings.APPName)
  43.        End If
  44.    End Sub




EDITO: Ya está, solucionado:

Código
  1. ....
  2. If Not playerargs = Nothing Then
  3.    ....
  4.    Dim Temp_file As String = System.IO.Path.GetTempPath & "\PlayList_temp.txt"
  5.  
  6.    Using objWriter As New System.IO.StreamWriter(Temp_file, false)
  7.        For Each folder In ArgsArray
  8.            If Not folder = Nothing Then
  9.                Dim di As New IO.DirectoryInfo(folder)
  10.                Dim files As IO.FileInfo() = di.GetFiles("*")
  11.                Dim file As IO.FileInfo
  12.                For Each file In files
  13.                    ' command to writleline
  14.                    'Console.WriteLine("File Name: {0}", file.Name)
  15.                    'Console.WriteLine("File Full Name: {0}", file.FullName)
  16.                    objWriter.Write(file.FullName)
  17.                    ' objWriter.Close()
  18.                Next
  19.            End If
  20.        Next
  21.    End Using ' Flush, close and dispose the objWriter
  22.    ....