private void buttonPublishBulletin_Click(object sender, EventArgs e)
        {
            var source              = openFileDialog2.FileName;
            var fileName            = openFileDialog2.SafeFileName;
            var destinationFileName = txtCaption.Text == String.Empty ? fileName : txtCaption.Text.Replace(" ", "_") +
                                      fileName.Substring(fileName.LastIndexOf("."));

            var destinationFolder = getBulletinsPagePath();

            var destination = String.Format("{0}/{1}", destinationFolder, destinationFileName);

            using (var ftp = new FtpConnection(m_ftpSite, m_ftpUsername, m_ftpPassword))
            {
                try
                {
                    ftp.Open();
                    ftp.Login();

                    if (!ftp.DirectoryExists(destinationFolder))
                    {
                        MessageBox.Show(
                            "The destination folder doesn't exist on the server. Please contact the BFI website administrator.");
                    }

                    ftp.PutFile(source, destination);
                    MessageBox.Show("Published bulletin successfully");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
示例#2
0
        private void SendAddOnToServer(List <string> files, String folderPath)
        {
            Console.WriteLine("Sending to server");
            TextReader tr   = new StreamReader(@"C:\Users\Pilus\Documents\Cloud Backup\PrologueServer.txt");
            string     addr = tr.ReadLine();
            string     user = tr.ReadLine();
            string     pass = tr.ReadLine();


            using (FtpConnection ftp = new FtpConnection(addr, user, pass)) {
                ftp.Open();  /* Open the FTP connection */
                ftp.Login(); /* Login using previously provided credentials */

                int c          = 0;
                int c2         = 0;
                int transfered = 0;

                foreach (String file in files)
                {
                    try {
                        // Check if the directory exists
                        String relativeFilePath = file.Replace(folderPath, "").Replace(@"\", "/");
                        String totalDirPath     = relativeFilePath.Substring(0, relativeFilePath.LastIndexOf("/"));
                        String folder           = totalDirPath.Substring(totalDirPath.LastIndexOf("/") + 1);
                        String topDir           = "/" + totalDirPath.Replace("/" + folder, "");
                        if (!ftp.GetCurrentDirectory().Equals("/" + totalDirPath))
                        {
                            ftp.SetCurrentDirectory(topDir);
                            if (!ftp.DirectoryExists(folder))
                            {
                                ftp.CreateDirectory(folder);
                            }
                        }



                        if (PutFile(ftp, file) == true)
                        {
                            transfered++;
                        }

                        c++;
                        int c3 = (c / (files.Count / 20));
                        if (c3 > c2)
                        {
                            Console.Write("{0}% ", c3 * 5);
                            c2 = c3;
                        }
                    }
                    catch (FtpException e) {
                        Console.WriteLine(String.Format("FTP Error: {0} {1}", e.ErrorCode, e.Message));
                    }
                }

                Console.WriteLine("{0} files created/updated.", transfered);
            }
        }
示例#3
0
        public static bool DirectoryExists(string path)
        {
            FtpConnection conn = GetFtpConnection();

            conn.Open();
            conn.Login();
            bool exists = conn.DirectoryExists(path);

            conn.Close();
            conn.Dispose();
            return(exists);
        }
示例#4
0
        public void PublishToGitFTP(DeploymentModel model)
        {
            if (model.AzureDeployment)
            {
                var remoteProcess =
                    Process.Start("\"" + gitLocation + "\"", " --git-dir=\"" + fullRepoPath + "\" remote add blog " + model.AzureRepo);
                if (remoteProcess != null)
                {
                    remoteProcess.WaitForExit();
                }

                var pushProcess = Process.Start("\"" + gitLocation + "\"", " --git-dir=\"" + fullRepoPath + "\" push -f blog master");
                if (pushProcess != null)
                {
                    pushProcess.WaitForExit();
                }
            }
            else
            {
                using (ftp = new FtpConnection(model.FTPServer, model.FTPUsername, model.FTPPassword))
                {
                    try
                    {
                        ftp.Open();
                        ftp.Login();

                        if (!string.IsNullOrWhiteSpace(model.FTPPath))
                        {
                            if (!ftp.DirectoryExists(model.FTPPath))
                            {
                                ftp.CreateDirectory(model.FTPPath);
                            }

                            ftp.SetCurrentDirectory(model.FTPPath);
                        }

                        FtpBlogFiles(snowPublishPath, model.FTPPath);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
示例#5
0
        public static void getFile(ref string UserName, ref string PassWord, ref string ServerName, ref string FileName)
        {
            using (FtpConnection ftp = new FtpConnection("ServerName", "UserName", "PassWord"))
            {
                ftp.Open();                                   /* Open the FTP connection */
                ftp.Login();                                  /* Login using previously provided credentials */

                if (ftp.DirectoryExists("/incoming"))         /* check that a directory exists */
                {
                    ftp.SetCurrentDirectory("/incoming");     /* change current directory */
                }
                if (ftp.FileExists("/incoming/file.txt"))     /* check that a file exists */
                {
                    ftp.GetFile("/incoming/file.txt", false); /* download /incoming/file.txt as file.txt to current executing directory, overwrite if it exists */
                }
                //do some processing

                try
                {
                    ftp.SetCurrentDirectory("/outgoing");
                    ftp.PutFile(@"c:\localfile.txt", "file.txt"); /* upload c:\localfile.txt to the current ftp directory as file.txt */
                }
                catch (FtpException e)
                {
                    Console.WriteLine(String.Format("FTP Error: {0} {1}", e.ErrorCode, e.Message));
                }

                foreach (var dir in ftp.GetDirectories("/incoming/processed"))
                {
                    Console.WriteLine(dir.Name);
                    Console.WriteLine(dir.CreationTime);
                    foreach (var file in dir.GetFiles())
                    {
                        Console.WriteLine(file.Name);
                        Console.WriteLine(file.LastAccessTime);
                    }
                }
            }
        } // End getFile()
示例#6
0
        private void FtpBlogFiles(string dirPath, string uploadPath)
        {
            string[] files   = Directory.GetFiles(dirPath, "*.*");
            string[] subDirs = Directory.GetDirectories(dirPath);


            foreach (string file in files)
            {
                ftp.PutFile(file, Path.GetFileName(file));
            }

            foreach (string subDir in subDirs)
            {
                if (!ftp.DirectoryExists(uploadPath + "/" + Path.GetFileName(subDir)))
                {
                    ftp.CreateDirectory(uploadPath + "/" + Path.GetFileName(subDir));
                }

                ftp.SetCurrentDirectory(uploadPath + "/" + Path.GetFileName(subDir));

                FtpBlogFiles(subDir, uploadPath + "/" + Path.GetFileName(subDir));
            }
        }
示例#7
0
        public bool connect(string user, string pass)
        {
            bool ret = false;

            logger.pushOperation("FTP.connect");
            try
            {
                sFTP = new FtpConnection(host, port);
                sFTP.Open();
                sFTP.Login(user, pass);
                ret = sFTP.DirectoryExists("/");
            }
            catch (Exception e)
            {
                ret = false;
                logger.log("Erro ao conectar FTP: " + e.Message, Logger.LogType.ERROR, e, false);
            }
            finally
            {
                logger.releaseOperation();
            }

            return(ret);
        }
示例#8
0
        private ActionResult FtpUpload(IJob job)
        {
            var actionResult = Check(job.Profile);

            if (!actionResult)
            {
                Logger.Error("Canceled FTP upload action.");
                return(actionResult);
            }

            if (string.IsNullOrEmpty(job.Passwords.FtpPassword))
            {
                Logger.Error("No ftp password specified in action");
                return(new ActionResult(ActionId, 102));
            }

            Logger.Debug("Creating ftp connection.\r\nServer: " + job.Profile.Ftp.Server + "\r\nUsername: "******"Can not connect to the internet for login to ftp. Win32Exception Message:\r\n" +
                                 ex.Message);
                    ftp.Close();
                    return(new ActionResult(ActionId, 108));
                }

                Logger.Error("Win32Exception while login to ftp server:\r\n" + ex.Message);
                ftp.Close();
                return(new ActionResult(ActionId, 104));
            }
            catch (Exception ex)
            {
                Logger.Error("Exception while login to ftp server:\r\n" + ex.Message);
                ftp.Close();
                return(new ActionResult(ActionId, 104));
            }

            var fullDirectory = job.TokenReplacer.ReplaceTokens(job.Profile.Ftp.Directory).Trim();

            if (!IsValidPath(fullDirectory))
            {
                Logger.Warn("Directory contains invalid characters \"" + fullDirectory + "\"");
                fullDirectory = MakeValidPath(fullDirectory);
            }

            Logger.Debug("Directory on ftp server: " + fullDirectory);

            var directories = fullDirectory.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                foreach (var directory in directories)
                {
                    if (!ftp.DirectoryExists(directory))
                    {
                        Logger.Debug("Create folder: " + directory);
                        ftp.CreateDirectory(directory);
                    }

                    Logger.Debug("Move to: " + directory);
                    ftp.SetCurrentDirectory(directory);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception while setting directory on ftp server\r\n:" + ex.Message);
                ftp.Close();
                return(new ActionResult(ActionId, 105));
            }

            var addendum = "";

            if (job.Profile.Ftp.EnsureUniqueFilenames)
            {
                Logger.Debug("Generate addendum for unique filename");
                try
                {
                    addendum = AddendumForUniqueFilename(Path.GetFileName(job.OutputFiles[0]), ftp);
                    Logger.Debug("The addendum for unique filename is \"" + addendum +
                                 "\" If empty, the file was already unique.");
                }
                catch (Exception ex)
                {
                    Logger.Error("Exception while generating unique filename\r\n:" + ex.Message);
                    ftp.Close();
                    return(new ActionResult(ActionId, 106));
                }
            }

            foreach (var file in job.OutputFiles)
            {
                try
                {
                    var targetFile = Path.GetFileNameWithoutExtension(file) + addendum + Path.GetExtension(file);
                    ftp.PutFile(file, MakeValidPath(targetFile));
                }
                catch (Exception ex)
                {
                    Logger.Error("Exception while uploading the file \"" + file + "\": \r\n" + ex.Message);
                    ftp.Close();
                    return(new ActionResult(ActionId, 107));
                }
            }

            ftp.Close();
            return(new ActionResult());
        }
 public bool DirectoryExists(string path)
 {
     return(_ftpConnection.DirectoryExists(path));
 }
示例#10
0
        private bool DownloadArquivos(BackgroundWorker status, string pasta, string subpasta)
        {
            tamanho             = 0;
            subPastaSelecionada = subpasta;
            try
            {
                if (subpasta.Length > 11 && subpasta.Substring(0, 12).Equals("ScriptsSQL/"))
                {
                    if (this.DownloadScriptSQLProgressBarControl.InvokeRequired)
                    {
                        DownloadScriptSQLProgressBarControl.Invoke(new MethodInvoker(delegate
                        {
                            this.DownloadScriptSQLProgressBarControl.EditValue = 0;
                            this.DownloadScriptSQLProgressBarControl.Update();
                        }));
                    }
                }
                else if (subpasta.Equals("Sistema"))
                {
                    if (this.CompSistemaProgressBarControl.InvokeRequired)
                    {
                        CompSistemaProgressBarControl.Invoke(new MethodInvoker(delegate
                        {
                            this.CompSistemaProgressBarControl.EditValue = 0;
                            this.CompSistemaProgressBarControl.Update();
                        }));
                    }
                }
                else if (subpasta.Equals("DevExpress") ||
                         subpasta.Equals("postgres/93") ||
                         subpasta.Equals("pt-br"))
                {
                    if (this.CompExternoProgressBarControl.InvokeRequired)
                    {
                        CompExternoProgressBarControl.Invoke(new MethodInvoker(delegate
                        {
                            this.CompExternoProgressBarControl.EditValue = 0;
                            this.CompExternoProgressBarControl.Update();
                        }));
                    }
                }

                if (ftp.DirectoryExists("/" + subpasta))
                {
                    ftp.SetCurrentDirectory("/" + subpasta);
                }

                var dir = ftp.GetCurrentDirectoryInfo();
                tamanho = dir.GetFiles().Length;

                foreach (var file in dir.GetFiles())
                {
                    if (!File.Exists(pasta + file.Name) || subpasta.Equals("ScriptsSQL"))
                    {
                        SetText("[" + file.Name + "]");
                        ftp.GetFile("/" + subpasta + "/" + file.Name, pasta + file.Name, false);
                    }
                    else
                    {
                        long     size  = TamanhoArquivoServidor(subpasta, file.Name);
                        FileInfo f     = new FileInfo(pasta + file.Name);
                        long     size2 = f.Length;

                        //COMPARAR ARQUIVOS DO SERVIDOR/ARQUIVOS LOCAIS
                        if (size != size2)
                        {
                            SetText("[" + file.Name + "]");
                            ftp.GetFile("/" + subpasta + "/" + file.Name, pasta + file.Name, false);
                        }
                        else
                        {
                            if (subpasta.Length > 11 && subpasta.Substring(0, 12).Equals("ScriptsSQL/"))
                            {
                                DownloadScriptSQLProgressBarControl.Invoke(new MethodInvoker(delegate
                                {
                                    this.DownloadScriptSQLProgressBarControl.Properties.Maximum = tamanho;
                                }));
                            }
                            else if (subpasta.Equals("Sistema"))
                            {
                                CompSistemaProgressBarControl.Invoke(new MethodInvoker(delegate
                                {
                                    this.CompSistemaProgressBarControl.Properties.Maximum = tamanho;
                                }));
                            }
                            else if (subpasta.Equals("DevExpress") ||
                                     subpasta.Equals("postgres/93") ||
                                     subpasta.Equals("pt-br"))
                            {
                                if (this.CompExternoProgressBarControl.InvokeRequired)
                                {
                                    CompExternoProgressBarControl.Invoke(new MethodInvoker(delegate
                                    {
                                        this.CompExternoProgressBarControl.Properties.Maximum = tamanho;
                                    }));
                                }
                            }
                        }

                        if (subpasta.Equals("Sistema"))
                        {
                            UpdateBarraArquivosSistema(tamanho);
                        }
                        else if (subpasta.Length > 11 && subpasta.Substring(0, 12).Equals("ScriptsSQL/1"))
                        {
                            UpdateBarraArquivosDownloadScripts(tamanho);
                        }
                        else if (subpasta.Equals("DevExpress") ||
                                 subpasta.Equals("postgres/93") ||
                                 subpasta.Equals("pt-br"))
                        {
                            UpdateBarraArquivosExternos(tamanho);
                        }

                        SetText("");
                    }
                }
            }
            catch
            {
                MessageBox.Show("O Sistema identificou que a conexão com o servidor não foi possível. Por favor, tente mais tarde.", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }
            return(true);
        }