quarta-feira, 29 de julho de 2015

ServerUpdater FTP C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;


namespace Lapuinka.ServerUpdater
{
   
    class AndroidXML
    {
        private DirectoryInfo dir = new DirectoryInfo(@"./");
        private XmlWriter writer = null;

        public bool createXML()
        {
            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.IndentChars = "\t";

                writer = XmlWriter.Create("xmlServer2.xml", settings);

     
                writer.WriteStartElement("root");

                foreach (DirectoryInfo tempRaiz in dir.GetDirectories())
                {
                    if (tempRaiz.GetFiles().Length > 0)
                    {
                        Console.WriteLine("Erro processando " + tempRaiz.Name);
                        Console.WriteLine("A pasta contem arquivos. Somente diretórios são permitidos");
                        return false;
                    }

                    String tempNameType = tempRaiz.Name;

            
                    foreach (DirectoryInfo typeArq in tempRaiz.GetDirectories())
                    {
                        if (typeArq.GetFiles().Length > 0)
                        {
                            Console.WriteLine("Erro processando " + tempNameType + "/" + typeArq.Name);
                            Console.WriteLine("A pasta contem arquivos. Somente diretórios são permitidos");
                            return false;
                        }
                        if (typeArq.Name.Contains(' ') || typeArq.Name.Contains('á') || typeArq.Name.Contains('é')
                            || typeArq.Name.Contains('í') || typeArq.Name.Contains('ó') || typeArq.Name.Contains('ú')
                            || typeArq.Name.Contains('Á') || typeArq.Name.Contains('É') || typeArq.Name.Contains('Í')
                            || typeArq.Name.Contains('Ó') || typeArq.Name.Contains('Ú') || typeArq.Name.Contains('â')
                            || typeArq.Name.Contains('ê') || typeArq.Name.Contains('î') || typeArq.Name.Contains('ô')
                            || typeArq.Name.Contains('û') || typeArq.Name.Contains('Â') || typeArq.Name.Contains('Ê')
                            || typeArq.Name.Contains('Î') || typeArq.Name.Contains('Ô') || typeArq.Name.Contains('Û')
                            || typeArq.Name.Contains('ã') || typeArq.Name.Contains('õ') || typeArq.Name.Contains('Ã')
                            || typeArq.Name.Contains('Õ') || typeArq.Name.Contains('ä') || typeArq.Name.Contains('ë')
                            || typeArq.Name.Contains('ï') || typeArq.Name.Contains('ö') || typeArq.Name.Contains('ü')
                            || typeArq.Name.Contains('Ä') || typeArq.Name.Contains('Ë') || typeArq.Name.Contains('Ï')
                            || typeArq.Name.Contains('Ö') || typeArq.Name.Contains('Ü') || typeArq.Name.Contains('ç')
                            || typeArq.Name.Contains('Ç'))
                        {
                            Console.WriteLine("Erro processando " + tempNameType);
                            Console.WriteLine("Os folders não podem conter espaço ou caracteres especiais: " + typeArq);
                            return false;
                        }
                        writer.WriteStartElement("file");
                        writer.WriteAttributeString("name", tempNameType + "/" + typeArq.Name);
                        String tempNameIMG = typeArq.Name;

                       
                        foreach (DirectoryInfo processName in typeArq.GetDirectories())
                        {
                            if ((!processName.Name.Equals("Copa") && !processName.Name.StartsWith("estadio_") && !processName.Name.StartsWith("regional_")))
                            {
                                Console.WriteLine("Erro processando " + tempNameType + "/" + typeArq.Name + "/" + processName.Name);
                                Console.WriteLine("O nome do estadio tem que ser 'Copa' ou começar com 'estadio_' ou 'regional_'");
                                return false;
                            }
                            if (typeArq.Name.Contains(' ') || typeArq.Name.Contains('á') || typeArq.Name.Contains('é')
                            || typeArq.Name.Contains('í') || typeArq.Name.Contains('ó') || typeArq.Name.Contains('ú')
                            || typeArq.Name.Contains('Á') || typeArq.Name.Contains('É') || typeArq.Name.Contains('Í')
                            || typeArq.Name.Contains('Ó') || typeArq.Name.Contains('Ú') || typeArq.Name.Contains('â')
                            || typeArq.Name.Contains('ê') || typeArq.Name.Contains('î') || typeArq.Name.Contains('ô')
                            || typeArq.Name.Contains('û') || typeArq.Name.Contains('Â') || typeArq.Name.Contains('Ê')
                            || typeArq.Name.Contains('Î') || typeArq.Name.Contains('Ô') || typeArq.Name.Contains('Û')
                            || typeArq.Name.Contains('ã') || typeArq.Name.Contains('õ') || typeArq.Name.Contains('Ã')
                            || typeArq.Name.Contains('Õ') || typeArq.Name.Contains('ä') || typeArq.Name.Contains('ë')
                            || typeArq.Name.Contains('ï') || typeArq.Name.Contains('ö') || typeArq.Name.Contains('ü')
                            || typeArq.Name.Contains('Ä') || typeArq.Name.Contains('Ë') || typeArq.Name.Contains('Ï')
                            || typeArq.Name.Contains('Ö') || typeArq.Name.Contains('Ü') || typeArq.Name.Contains('ç')
                            || typeArq.Name.Contains('Ç'))
                            {
                                Console.WriteLine("Erro processando " + tempNameType);
                                Console.WriteLine("Os folders não podem conter espaço ou caracteres especiais: " + typeArq);
                                return false;
                            }
                            if (processName.GetFiles().Length == 1)
                            {
                                String fileName = processName.GetFiles().First().Name;
                                if (!fileName.Equals(typeArq.Name))
                                {
                                    Console.WriteLine("Erro processando " + tempNameType + "/" + typeArq.Name + "/" + processName.Name + "/" + fileName);
                                    Console.WriteLine("O nome do arquivo tem que ser o mesmo da pasta: " + fileName + typeArq);
                                    return false;
                                }
                                writer.WriteStartElement(processName.Name);
                                writer.WriteAttributeString("version", processName.GetFiles().First().LastWriteTime.ToString());
                                writer.WriteEndElement();
                            }
                            else if (processName.GetFiles().Length == 0)
                            {
                                Console.WriteLine("Erro processando " + tempNameType + "/" + typeArq.Name + "/" + processName.Name);
                                Console.WriteLine("A pasta esta vazia");
                                return false;
                            }
                            else
                            {
                                Console.WriteLine("Erro processando " + tempNameType + "/" + typeArq.Name + "/" + processName.Name);
                                Console.WriteLine("A pasta contem mais de um arquivo");
                                return false;
                            }
                        }
                        writer.WriteEndElement();
                    }
                }
                writer.WriteEndElement();
                writer.Flush();
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Erro: " + e.Message);
                return false;
            }
            finally
            {
                if (writer != null) {  
                }
                writer.Close();
            }
        }
    }          


    class Program
    {
        static void Main(string[] args)
        {          
            AndroidXML serverXML = new AndroidXML();
            if (serverXML.createXML())
            {
                FileInfo fiNew = new FileInfo("xmlServer2.xml");
                FileInfo fiOld = new FileInfo("xmlServer.xml");
                if (fiOld.Exists)
                {
                    fiOld.Delete();
                }
                fiNew.MoveTo("xmlServer.xml");
                Console.WriteLine("Arquivo processado com sucesso");
            }
            Console.ReadKey();
        }  
    }


}
Postar um comentário