Exemple #1
0
        private void HandleChange(DirectoryChangeEventType changeType, string fileName = "", string fullName = "",
                                  string oldName = "", string oldPath = "",
                                  string newName = "", string newPath = "")
        {
            switch (changeType)
            {
            case DirectoryChangeEventType.OnChange:
            {
                var destinationFile = Path.Combine(OutputDir, fileName);

                if (File.Exists(fullName))
                {
                    CreateFileDirectoryIfNotExists(fileName);

                    try
                    {
                        File.Copy(fullName, destinationFile, true);
                        Log.Information("[MODIFICAÇÃO] {0}, no local {1} foi modificado.", fileName, destinationFile);
                    }
                    catch (Exception e)
                    {
                        HandleError(e);
                    }
                }
                else
                {
                    Log.Information("[MODIFICAÇÃO] Arquivo {0} não existe no diretório de origem.", fullName);
                }

                ChangeLabel("lastChangeLabel", DateTime.Now.ToShortTimeString(), Color.Green);
                ChangeLabel("statusLabel", "Está sincronizando", Color.Green);
                break;
            }

            case DirectoryChangeEventType.OnCreate:
            {
                var destinationFile = Path.Combine(OutputDir, fileName);

                if (File.Exists(fullName))
                {
                    CreateFileDirectoryIfNotExists(fileName);

                    try
                    {
                        File.Copy(fullName, destinationFile);
                        Log.Information("[CRIAÇÃO] {0}, no local {1} foi criado.", fileName, destinationFile);
                    }
                    catch (Exception e)
                    {
                        HandleError(e);
                    }
                }
                else
                {
                    Log.Information("[CRIAÇÃO] Arquivo {0} não existe no diretório de origem.", fullName);
                }

                ChangeLabel("lastChangeLabel", DateTime.Now.ToShortTimeString(), Color.Green);
                ChangeLabel("statusLabel", "Está sincronizando", Color.Green);
                break;
            }

            case DirectoryChangeEventType.OnCreateDir:
            {
                var destinationDir = Path.Combine(OutputDir, fileName);

                if (!Directory.Exists(destinationDir))
                {
                    try
                    {
                        Directory.CreateDirectory(destinationDir);
                        Log.Information("[CRIAÇÃO DE DIRETÓRIO] {0}, no local {1} foi criado.", fileName, destinationDir);
                    }
                    catch (Exception e)
                    {
                        HandleError(e);
                    }
                }
                else
                {
                    Log.Information("[CRIAÇÃO DE DIRETÓRIO] Diretório {0} já existe.", destinationDir);
                }

                ChangeLabel("lastChangeLabel", DateTime.Now.ToShortTimeString(), Color.Green);
                ChangeLabel("statusLabel", "Está sincronizando", Color.Green);
                break;
            }

            case DirectoryChangeEventType.OnDelete:
            {
                var destinationFile = Path.Combine(OutputDir, fileName);

                if (File.Exists(destinationFile))
                {
                    try
                    {
                        File.Delete(destinationFile);
                        Log.Information("[DELEÇÃO] {0}, no local {1} foi deletado.", fileName, destinationFile);
                    }
                    catch (Exception e)
                    {
                        HandleError(e);
                    }
                }
                else
                {
                    Log.Information("[DELEÇÃO] Arquivo {0} não existe no diretório de origem.", destinationFile);
                }

                ChangeLabel("lastChangeLabel", DateTime.Now.ToShortTimeString(), Color.Green);
                ChangeLabel("statusLabel", "Está sincronizando", Color.Green);
                break;
            }

            case DirectoryChangeEventType.OnDeleteDir:
            {
                var destinationDir = Path.Combine(OutputDir, fileName);

                if (Directory.Exists(destinationDir))
                {
                    try
                    {
                        var dirToDelete = new DirectoryInfo(destinationDir);
                        dirToDelete.Delete(true);
                        Log.Information("[DELEÇÃO DE DIRETÓRIO] {0}, no local {1} foi deletado.", fileName, destinationDir);
                    }
                    catch (Exception e)
                    {
                        HandleError(e);
                    }
                }

                else
                {
                    Log.Information("[DELEÇÃO DE DIRETÓRIO] Diretório {0} não existe.", destinationDir);
                }

                ChangeLabel("lastChangeLabel", DateTime.Now.ToShortTimeString(), Color.Green);
                ChangeLabel("statusLabel", "Está sincronizando", Color.Green);
                break;
            }

            case DirectoryChangeEventType.OnRename:
            {
                var destinationOldName = Path.Combine(OutputDir, oldName);
                var destinationNewName = Path.Combine(OutputDir, newName);

                if (File.Exists(destinationOldName))
                {
                    try
                    {
                        File.Move(destinationOldName, destinationNewName);
                        Log.Information(" {0} foi renomeado para {1}", destinationOldName, destinationNewName);
                    }
                    catch (Exception e)
                    {
                        HandleError(e);
                    }
                }
                else
                {
                    Log.Information("[RENOMEAÇÃO] Arquivo {0} não existe no diretório de origem.", destinationOldName);
                }

                ChangeLabel("lastChangeLabel", DateTime.Now.ToShortTimeString(), Color.Green);
                ChangeLabel("statusLabel", "Está sincronizando", Color.Green);
                break;
            }

            case DirectoryChangeEventType.OnRenameDir:
            {
                var destinationOldName = Path.Combine(OutputDir, oldName);
                var destinationNewName = Path.Combine(OutputDir, newName);

                if (Directory.Exists(destinationOldName))
                {
                    try
                    {
                        Directory.Move(destinationOldName, destinationNewName);
                        Log.Information(" {0} foi renomeado para {1}", destinationOldName, destinationNewName);
                    }
                    catch (Exception e)
                    {
                        HandleError(e);
                    }
                }
                else
                {
                    Log.Information("[RENOMEAÇÃO DE DIRETÓRIO] Diretório {0} não existe no diretório de origem.", destinationOldName);
                }

                ChangeLabel("lastChangeLabel", DateTime.Now.ToShortTimeString(), Color.Green);
                ChangeLabel("statusLabel", "Está sincronizando", Color.Green);
                break;
            }

            case DirectoryChangeEventType.OnFullSave:
            {
                DirUtils.DirectoryCopy(InputDir, OutputDir, true, true);
                break;
            }

            default:
                HandleError(new ArgumentOutOfRangeException(nameof(changeType), changeType, null));
                break;
            }
        }