Quantcast
Channel: Dúvida referente a criação de classe
Viewing all articles
Browse latest Browse all 4

Dúvida referente a criação de classe

0
0

Boa tarde, tudo bem ?

Como eu poderia transformar o código abaixo em uma classe ?

O que preciso é da string retornada para apenas exibir no listbox.

private void MonitoraPasta()
        {
            //instânciando a classe e passando a pasta a ser monitorada
            FileSystemWatcher fsw = new FileSystemWatcher(@"D:\DMAX");

            //incluindo as subpastas, para também serem monitoridas
            fsw.IncludeSubdirectories = false;

            //Através de um Enum dizemos quais atributos devem ser monitorados, modificação da data do arquivo, tamanho, etc...
            fsw.NotifyFilter = NotifyFilters.FileName |
                NotifyFilters.CreationTime |
                NotifyFilters.Size;
                /*
                |  NotifyFilters.LastWrite             
                | NotifyFilters.DirectoryName
                | NotifyFilters.LastAccess
                | NotifyFilters.Attributes          
                | NotifyFilters.Security;
                 */

            //Filtro de arquivos que serão monitorados
            fsw.Filter = "*.txt*";

            //A propriedade abaixo define que a monitoração deve iniciar, se false, a pasta não será monitorada
            fsw.EnableRaisingEvents = true;
            CheckForIllegalCrossThreadCalls = false;

            //WaitForChangedResult
            
            //Eventos monitorados
            fsw.Changed += new FileSystemEventHandler(fsw_Changed);
            fsw.Created += new FileSystemEventHandler(fsw_Created);
            fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
            fsw.Renamed += new RenamedEventHandler(fsw_Renamed);
            fsw.Error += new ErrorEventHandler(fsw_Error);
        }

        private void MostraTextoStatusArquivo(string text)
        {
            lbMonitor.Items.Add(text);
        }

        void fsw_Renamed(object sender, RenamedEventArgs e)
        {
            try
            {
                lbMonitor.BeginInvoke(new AdicionaTexto(MostraTextoStatusArquivo),
                    new object[] { e.Name + " - " + e.ChangeType + ": " + e.FullPath });
            }
            catch (Exception ex)
            {
                lbMonitor.BeginInvoke(new AdicionaTexto(MostraTextoStatusArquivo),
                    new object[] { "Ocorreu um erro" +ex.Message });
            }

        }

        void fsw_Deleted(object sender, FileSystemEventArgs e)
        {
            try
            {
            lbMonitor.BeginInvoke(new AdicionaTexto(MostraTextoStatusArquivo),
                new object[] { e.Name + " - " + e.ChangeType + ": " + e.FullPath });
                 }
            catch (Exception ex)
            {
                lbMonitor.BeginInvoke(new AdicionaTexto(MostraTextoStatusArquivo),
                    new object[] { "Ocorreu um erro" +ex.Message });
            }
        }

        void fsw_Created(object sender, FileSystemEventArgs e)
        {
            try
            {
            lbMonitor.BeginInvoke(new AdicionaTexto(MostraTextoStatusArquivo),
                new object[] { e.Name + " - " + e.ChangeType + ": " + e.FullPath });
                 }
            catch (Exception ex)
            {
                lbMonitor.BeginInvoke(new AdicionaTexto(MostraTextoStatusArquivo),
                    new object[] { "Ocorreu um erro" +ex.Message });
            }
        }

        void fsw_Changed(object sender, FileSystemEventArgs e)
        {
            try
            {
            lbMonitor.BeginInvoke(new AdicionaTexto(MostraTextoStatusArquivo),
                new object[] { e.Name + " - " + e.ChangeType + ": " + e.FullPath });
            }
            catch (Exception ex)
            {
                lbMonitor.BeginInvoke(new AdicionaTexto(MostraTextoStatusArquivo),
                    new object[] { "Ocorreu um erro" + ex.Message });
            }
        }

        private void fsw_Error(object sender, ErrorEventArgs e)
        {
           
        }

        //Adicionaremos um delegate que será responsável de pegar os parâmetros do método MostraTextoStatusArquivo como ponteiro de outra função: 
        public delegate void AdicionaTexto(string str);






        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MonitoraPasta();
        }
Obrigado


Viewing all articles
Browse latest Browse all 4

Latest Images





Latest Images