监视一个目录,做成windows服务后,当有txt文件产生时,就扫描txt的内容,根据不同的记号分别把文件拷贝到不同的目录去.可服务总是只执行一次.你们谁遇到过这样的事情?

解决方案 »

  1.   

    code在windows应用程序里试了,没问题.
      

  2.   

    Me.FileSystemWatcher1.EnableRaisingEvents = True
    Me.FileSystemWatcher1.Filter = "*.txt"
    Me.FileSystemWatcher1.NotifyFilter = System.IO.NotifyFilters.LastWrite
    Me.FileSystemWatcher1.Path = "D:\Data-Keyed\200506\0606"Private strSourceDirPath As String = "D:\Data-Keyed\200506\0606"
    Private strDestiQmDirPath As String = "D:\Data-Qm\200506\0606"
    Private strDestiNonQmDirPath As String = "D:\Data-NonQm\200506\0606"
    Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
            'e.FullPath是txt文件的包括绝对路径的全名
            ScanTheCreatedTxtFile(e.FullPath)    End Sub    Private Sub ScanTheCreatedTxtFile(ByVal FullFileName As String)
            
            Dim strFileName As String
            strFileName = FullFileName.Substring(FullFileName.LastIndexOf("\") + 1,    FullFileName.Length - FullFileName.LastIndexOf("\") - 1)        Dim sr As StreamReader = New StreamReader(FullFileName)
            Dim line As String
            ' Read and display the lines from the file until the end 
            ' of the file is reached.
            Do
                line = sr.ReadLine()
                If line Is Nothing Then
                    '没问号
                    sr.Close()
                    File.Copy(FullFileName, strDestiNonQmDirPath & "\" & strFileName)
                    Exit Do
                ElseIf line.IndexOfAny("?") <> -1 Then
                    '有问号
                    sr.Close()
                    File.Copy(FullFileName, strDestiQmDirPath & "\" & strFileName)
                    Exit Do
                End If        Loop Until line Is Nothing        sr.Close()    End Sub