(VB.NET, .NET 3.5)Strange "IOException était non gérée"
j'ai écrit la fonction suivante pour lire un texte à partir du fichier txt. Cela fonctionnait bien mais maintenant ce n'est pas le cas. Il continue à me donner ce message d'erreur
« IOException était non gérée » et
« Le processus ne peut pas accéder au fichier « F: \ kh_matt \ ch1.txt » parce qu'il est utilisé par un autre processus. »
Le fichier ch1.txt n'est même pas ouvert ou utilisé par un programme. J'ai essayé de déplacer ch1.txt à un autre emplacement (lecteur D) mais j'ai toujours la même erreur de message mais juste l'endroit différent qu'il indique Le processus ne peut pas accéder au dossier 'D: \ ch1.txt' parce qu'il est employé par un autre processus. "
Voici mon bloc de code:
Private Sub btnRead_Click (expéditeur de ByVal comme System.Object, ByVal e comme System.EventArgs) Poignées btnRead.Click
Dim reader As StreamReader
Dim filelocation As String
filelocation = "F:\kh_matt\ch1.txt"
Dim chid As Integer
chid = 1
If System.IO.File.Exists(filelocation) = True Then
reader = New StreamReader(New FileStream(filelocation, FileMode.Open))
Else
MsgBox(filelocation, MsgBoxStyle.OkOnly)
End If
Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
Dim vArray() As String = MyStream.ReadToEnd.Split(CChar("$"))
MyStream.Close()
Dim count As Integer
For d As Integer = 0 To vArray.Length - 1 Step 1
If d = vArray.Length - 1 Then
Exit For
End If
InsertKh(chid, d + 1, vArray(d))
count = d + 1
Next
MsgBox("Done Inserting")
End Sub
Il pointe toujours à ce code:
Dim M yStream en tant que nouveau StreamReader (Path.Combine (Application.StartupPath, filelocation))
Où je débogue et appuyez sur le bouton correspondant. Quelqu'un peut-il indiquer quel est le problème? Merci
Oui, vous avez raison. Maintenant, je commentais ce bloc de code: 'Si System.IO.File.Exists (emplacementfichier) = True Then ' lecteur = New StreamReader (New FileStream (emplacementfichier, FileMode.Open)) 'Else ' MsgBox (emplacementfichier , MsgBoxStyle.OkOnly) 'End If –