public Main()
        {
            InitializeComponent();

            //initialize tray icon
            notifyIcon = new NotifyIcon();
            Stream iconStream = System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/clientWPF;component/sync.ico")).Stream;
            notifyIcon.Icon = new System.Drawing.Icon(iconStream);
            notifyIconMenu = new System.Windows.Forms.ContextMenu();
            System.Windows.Forms.MenuItem mnuItemSyncNow = new System.Windows.Forms.MenuItem();
            System.Windows.Forms.MenuItem mnuItemShow = new System.Windows.Forms.MenuItem();
            mnuItemShow.Text = "Show";
            mnuItemShow.Click += new System.EventHandler(notifyIcon_Click);
            notifyIconMenu.MenuItems.Add(mnuItemShow);
            mnuItemSyncNow.Text = "SyncNow";
            mnuItemSyncNow.Click += new System.EventHandler(syncMenuItem);
            notifyIconMenu.MenuItems.Add(mnuItemSyncNow);
            notifyIcon.Text = "SyncClient";
            notifyIcon.ContextMenu = notifyIconMenu;
            notifyIcon.BalloonTipTitle = "App minimized to tray";
            notifyIcon.BalloonTipText = "Sync sill running.";

            // Settings
            connectionSettings = new ConnectionSettings();
            tAddress.Text = connectionSettings.readSetting("connection", "address");
            tPort.Text = connectionSettings.readSetting("connection", "port");
            tDirectory.Text = connectionSettings.readSetting("account", "directory");
            tTimeout.Text = connectionSettings.readSetting("connection", "syncTime");
        }
        /// <summary>
        /// Inizializza il controllo periodico del contenuto della cartella.
        /// Controlla anche le credenziali con il server testando il login.
        /// </summary>
        /// <exception cref="ServerException"></exception>
        /// <exception cref="ClientException"></exception>
        public static void Inizializza()
        {
            connectionSetting = new ConnectionSettings();
            user = connectionSetting.readSetting("account", "username");
            pwd = connectionSetting.readSetting("account", "password");
            base_path = connectionSetting.readSetting("account", "directory");
            interval = Int32.Parse(connectionSetting.readSetting("connection", "syncTime"));

            if (!base_path.EndsWith("\\"))
            {
                base_path += "\\";
            }

            Properties.Settings.Default.user = user;
            Properties.Settings.Default.pwd = pwd;
            Properties.Settings.Default.base_path = base_path;
            Properties.Settings.Default.intervallo = interval;
            Properties.Settings.Default.Save();

            if (!init)
            {
                if (user == null || pwd == null)
                    throw new ClientException("Mancano le credenziali dell'utente. Fare il login.", ClientErrorCode.CredenzialiUtenteMancanti);
                if (base_path == null)
                    throw new ClientException("Il percorso di controllo non è specificato", ClientErrorCode.PercorsoNonSpecificato);
                //Controllo se nome utente e password sono giusti
                if (!Command.Logged)
                {
                    try
                    {
                        ComandoLogin login = new ComandoLogin(user, pwd);
                        login.esegui();
                        ComandoEsci esci = new ComandoEsci();
                        esci.esegui();
                    }
                    catch (ServerException e) //Se il server non è disponibile l'eccezione non viene catturata...
                    {
                        switch (e.ErrorCode)
                        {
                            case ServerErrorCode.DatiErrati:
                                throw new ClientException("Le credenziali dell'utente sono errate. Rifare il login o registrarsi.", ClientErrorCode.CredenzialiUtenteErrate);
                            default:
                                throw;
                        }
                    }
                }
                //Controlla se il DB è ok

                //if (!DB_Table.DBEsiste)
                    //throw new ClientException("Il database non esiste", ClientErrorCode.DatabaseNonPresente);
                /*
                string sha_db = DB_Table.HashDB;
                ComandoScaricaHashDB c = new ComandoScaricaHashDB();
                c.esegui();
                if(sha_db != c.hash)
                {
                    throw new ClientException("Il database è corrotto o non esiste");
                }
                */
                //Imposta il timer per il controllo periodico
                checker = new System.Timers.Timer(interval * 1000);
                checker.AutoReset = true;
                checker.Elapsed += Checker_Elapsed;
                init = true;
                if (!Directory.Exists(base_path))
                    throw new ClientException(Properties.Messaggi.CartellaNonEsistente, ClientErrorCode.CartellaNonEsistente);
            }
            checker.Enabled = true;
        }