IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

"form" pour détection d’accès à un fichier


Sujet :

VB.NET

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2011
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 9
    Points : 3
    Points
    3
    Par défaut "form" pour détection d’accès à un fichier
    hello world,
    je me suis lancé dans le codage d'une interface qui me permet de savoir quel fichier à été modifier/utiliser dans un répertoire.

    je suis encore débutant en vb net et en particulier au niveau des I/O.

    voici ci joint mon premier essais (infructueux, inutile de le dire)
    -l'interface est fonctionnel
    -la recherche du répertoire marche

    reste donc les problèmes duent à l'utilisation des I/O et ceux duent a la taille et au contenue de mon tableau.

    Pour voir le projet complet, voir fichier ci-joint

    Voila, si quelqu’un s'y connais bien en I/O avec VB net je pense qu'il sera facile de m'aider

    merci à vous
    Fichiers attachés Fichiers attachés

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 753
    Points
    39 753
    Par défaut
    Il y a peu de chances que quelqu'un se donne la peine de télécharger ton projet et d'en étudier tout le code... indique plutôt ce qui te pose problème exactement, avec l'extrait de code concerné

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2011
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 9
    Points : 3
    Points
    3
    Par défaut re : le code
    OK,
    voila le passage qui je pense pose problème :

    Tout d'abord la classe scanner qui a pour but de trouver le fichier modifiées

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Imports System.IO
    
    Public Class Scanner
        Dim emplacement As String
        Public Sub scanner(ByVal emplacement As String)
            Dim FolderL As New IO.DirectoryInfo(emplacement)
            Dim L As Integer
    
            L = 0
            For Each fichier As FileInfo In FolderL.GetFiles("*.*") ' a mon avis c'est pas correct ici
                L = L + 1
            Next
            L = L - 1
    
            Dim tableau(L, 3) As String
            Dim j As Integer
            j = 0
    
            For Each fichier As FileInfo In FolderL.GetFiles("*.*")
                tableau(j, 0) = fichier.Name
                tableau(j, 1) = fichier.Length
                tableau(j, 3) = 1
                j = j + 1
            Next
    
            Do While Form1.bscan = True
                For Each fichier As FileInfo In FolderL.GetFiles("*.*")
                    For i = 0 To L - 1
                        If tableau(i, 0) = fichier.Name Then
                            If fichier.Length = tableau(i, 1) Then
                                [COLOR="rgb(0, 100, 0)"]tableau(i, 3) = 0[/COLOR] 'un peu trop brutale ?
                            End If
                        End If
                    Next
                Next
    
                For i = 0 To L - 1
                    If tableau(i, 3) = 1 Then
                        Ajoutermodfi(tableau(i, 0))
                    End If
                Next
    
                L = 0
                For Each fichier As FileInfo In FolderL.GetFiles("*.*")
                    L = L + 1
                Next
                L = L - 1
    
                ReDim tableau(L, 3)
    
                j = 0
    
                For Each fichier As FileInfo In FolderL.GetFiles("*.*")
                    tableau(j, 0) = fichier.Name
                    tableau(j, 1) = fichier.Length
                    tableau(j, 3) = 0
                    j = j + 1
                Next
                Delay(5)
            Loop
    
        End Sub

    et la form

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button.Click
     
            If button.Text = "Commencer la surveillance" Then
                'Vérifie si le chemin est valide
                If TextBox.Text = "" Then
                    MsgBox("Chemin d'accès au dossier inexistant")
                    Exit Sub
                Else
                    button.Text = "Arreter la surveillance"
                    bscan = True
                    Dim S As New Scanner()
                    S.scanner(TextBox.Text)
                End If
            Else
                button.Text = "Commencer la surveillance"
                bscan = False
            End If
     
     
        End Sub
    (un petit crtl+c ctrl+v dans Notepad++ ne fera pas de mal je pense)

  4. #4
    Invité
    Invité(e)
    Par défaut
    Je ne me suis pas aussi casser la tête à télécharger ton projet et à y jeter un oeil.

    par contre j'aimerai savoir si t'as pensé à la classe FileSystemWatcher ?

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2011
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 9
    Points : 3
    Points
    3
    Par défaut
    merci à tomlev pour avoir clarifier mon message

    h2s84 : c'est exactement ce que je cherchais, je vais regarde tout ça et je reviens pour mettre le code une fois qu'il marche , ça pourra servir à d'autre.

    encore merci.

  6. #6
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2011
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 9
    Points : 3
    Points
    3
    Par défaut
    voila, j'ai fait des modif !
    le programme marche encore moins bien, mais cela me semble plus interressant comme code alors je vais essayer de corriger à partir de celui là

    j'ai trois soucis majeur :
    - je ne peut pas accéder à ma form tant que le scan est en cours
    - les fichier qui sont juste ouvert ne sont pas detecté par mon scanner
    - j'ai 3 icon dans ma barre de tache (un detail, mais cela reste un pb)

    voici le nouveau code (plus court):

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Imports System.IO
    Imports System.Security.Permissions
     
    Public Class Scanner
     
        <PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
        Public Sub scanner(ByVal emplacement)
            Dim watcher As New FileSystemWatcher()
            watcher.Path = emplacement
            ' Watch for changes in LastAccess and LastWrite times, and
            ' the renaming of files or directories. 
            watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.LastAccess)
            ' Only watch text files.
            watcher.Filter = "*.*"
     
            ' Add event handlers.
            AddHandler watcher.Changed, AddressOf OnChanged
            AddHandler watcher.Created, AddressOf OnChanged
            AddHandler watcher.Deleted, AddressOf OnChanged
     
            ' Begin watching.
            watcher.EnableRaisingEvents = True
     
            ' Wait for the user to quit the program.
            While Form1.bscan = True
            End While
        End Sub
     
        ' Define the event handlers.
        Public Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
            ' Specify what is done when a file is changed, created, or deleted.
            Ajoutermodfi(e.FullPath)
        End Sub
     
        Public Sub Ajoutermodfi(ByVal p1 As String)
            With Form1.NotifyIcon1
     
                .BalloonTipIcon = ToolTipIcon.Info ' Icône information de Windows.
                .BalloonTipTitle = "NotifyfromAntitrust" ' Titre du message.
                .BalloonTipText = "Le fichier : " & p1 & " vient d'être utilisé" ' Corps du message.
     
            End With
     
            Form1.NotifyIcon1.ShowBalloonTip(1)
     
            Form1.Label4.Text = Form1.Label3.Text
            Form1.Label3.Text = Form1.Label2.Text
            Form1.Label2.Text = Form1.Label1.Text
            Form1.Label1.Text = p1
        End Sub
    End Class
    voila et maintenant le code de la form

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Imports System.Threading
    Imports System.IO
     
    Public Class Form1
        Public bscan As Boolean
     
        Private Sub Buttonb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonb.Click
            Dim folderDlg As New FolderBrowserDialog
            folderDlg.ShowNewFolderButton = True
            If (folderDlg.ShowDialog() = DialogResult.OK) Then
                TextBox.Text = folderDlg.SelectedPath
                Dim root As Environment.SpecialFolder = folderDlg.RootFolder
            End If
        End Sub
     
        Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button.Click
     
            If button.Text = "Commencer la surveillance" Then
                'Vérifie si le chemin est valide
                If TextBox.Text = "" Then
                    MsgBox("Chemin d'accès au dossier inexistant")
                    Exit Sub
                Else
                    button.Text = "Arreter la surveillance"
                    bscan = True
                    Me.Hide()
                    Dim S As New Scanner()
                    S.scanner(TextBox.Text)
                End If
            Else
                button.Text = "Commencer la surveillance"
                bscan = False
            End If
     
     
        End Sub
     
        Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.Click
            bscan = False
            Me.Show()
        End Sub
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.Hide()
        End Sub
    End Class

  7. #7
    Invité
    Invité(e)
    Par défaut
    J'en déduis que je t'ai sauvé vu que t'as failli réinventer la roue.

    Citation Envoyé par V8N37N3WB Voir le message
    - je ne peut pas accéder à ma form tant que le scan est en cours
    Ce sera mieux si tu passes par un BackgroundWorker.

    Citation Envoyé par V8N37N3WB Voir le message
    - les fichier qui sont juste ouvert ne sont pas detecté par mon scanner
    normal, d'après msdn : "Écoute les notifications de modifications du système de fichiers et déclenche un événement lorsqu'un répertoire ou un fichier d'un répertoire est modifié."

    Citation Envoyé par V8N37N3WB Voir le message
    - j'ai 3 icon dans ma barre de tache (un detail, mais cela reste un pb)
    je n'ai pas bien saisi ce problème.

  8. #8
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2011
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 9
    Points : 3
    Points
    3
    Par défaut
    Je vais étudier le backgroundworker plus en détail, cela régle certainement mon problème.
    Pour les icons je crois avoir comprit, je réglé le probléme !
    Reste donc un problème :
    - je cherche à voir le fichier qui ont été ouvert (ex : un pdf qui à été lu, un fichier excel ouvert mais pas enregistrer etc.)
    -> le watcher ne marche pas pour ce genre de demande alors ?
    Dois je faire un vérification des accès dd (est ce faisable en vbnet?)? connais tu des modules plus approprié ?

  9. #9
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 753
    Points
    39 753
    Par défaut
    Pas besoin d'un Backgroundworker... le problème c'est ton While Form1.bscan = True. Cette boucle ne sert à rien et bloque le thread sur lequel elle s'exécute. Enlève la et ça ira beaucoup mieux...

    Pour démarrer/arrêter le scan, pas besoin d'une variable bscan : utilise simplement la propriété EnableRaisingEvents du FileSystemWatcher

  10. #10
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par V8N37N3WB Voir le message
    - je cherche à voir le fichier qui ont été ouvert (ex : un pdf qui à été lu, un fichier excel ouvert mais pas enregistrer etc.)
    -> le watcher ne marche pas pour ce genre de demande alors ?
    Dois je faire un vérification des accès dd (est ce faisable en vbnet?)? connais tu des modules plus approprié ?
    Effectivement la classe FileSystemWatcher ne le permet pas. par contre tu peux essayer de voir la proprieté LastAccessTime de la classe FileInfo.

  11. #11
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2011
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 9
    Points : 3
    Points
    3
    Par défaut
    Idem même combat !
    j'en viens à me demander si Windows stock vraiment ça quelque part

    je m'occupe de mon backgroundworker plus tard (se soir je pense)

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo