VB.NET Erreur Fichier utiliser par un autre processus
Bonjour,
je me retrouve coincé depuis quelque jours sur une erreur que je n'arrive pas à résoudre :
1/ J'enregistre une liste de joueurs avec les ID et MDP dans des fichiers distinct.
2/ Je crypte les fichiers à l'aide d'une clé défini au préalable.
lorsque je décrypte les fichiers avec la bonne clé de décryptage tout se passe comme voulu mais lorsque je rentre volontairement une mauvaise clé je rencontre une erreur : "Le fichier est utiliser par un autre processus" au moment de la tentative de suppressions des fichiers ...
ci-joint le
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Decrypt()
Try
'L ERREUR SURVIENS ICI
'verifie l existance fichier et l efface
If IO.File.Exists("C:\CnC\login" & TextBoxJoueur.Text & "Encrypted.txt") Then
IO.File.Delete("C:\CnC\login" & TextBoxJoueur.Text & "Encrypted.txt")
End If
'verifie l existance fichier et l efface
If IO.File.Exists("C:\CnC\pass" & TextBoxJoueur.Text & "Encrypted.txt") Then
IO.File.Delete("C:\CnC\pass" & TextBoxJoueur.Text & "Encrypted.txt")
End If
Catch ex As Exception
MsgBox("Erreur :" & ex.Message, MsgBoxStyle.Critical, "Z!T¤ùN£ @NT!C!P-W!N-32")
End Try
End Sub
End Class
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Module ModuleDecrypt
'Doit correspondre à 64 bits, 8 octets.
Dim sSecretKey As String = Form1.TextBox1.Text
Sub Decrypt()
DecryptFile("C:\CnC\login" & Form1.TextBoxJoueur.Text & "Encrypted.txt", _
"C:\CnC\login" & Form1.TextBoxJoueur.Text & ".txt", _
sSecretKey)
DecryptFile("C:\CnC\pass" & Form1.TextBoxJoueur.Text & "Encrypted.txt", _
"C:\CnC\pass" & Form1.TextBoxJoueur.Text & ".txt", _
sSecretKey)
End Sub
Sub DecryptFile(ByVal sInputFilename As String, _
ByVal sOutputFilename As String, _
ByVal sKey As String)
Try
Dim DES As New DESCryptoServiceProvider()
'Une clé de 64 bits et un vecteur d'initialisation sont requis pour ce fournisseur.
'Définit la clé secrète pour l'algorithme DES.
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
'Définit le vecteur d'initialisation.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'crée un flux de fichier pour lire le fichier crypté de retour
Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
'crée un décrypteur DES à partir de l'instance DES
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
'crée un flux de cryptage, défini pour lire et effectuer une transformation
'de décryptage DES sur les octets entrants
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
'imprime le contenu du fichier décrypté
Dim fsDecrypted As New StreamWriter(sOutputFilename)
fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
fsDecrypted.Flush()
fsDecrypted.Close()
fsread.Close()
Catch ex As Exception
MsgBox("Erreur :" & ex.Message, MsgBoxStyle.Critical, "Z!T¤ùN£ @NT!C!P-W!N-32")
End Try
End Sub
End Module |
restant a disposition pour plus de renseignement si nécessaire et vous remerciant si possible d'éclairer ma lanterne :)