示例#1
0
        /// <summary>
        /// Prüft ein FTP-Verzeichnis und gibt Grün wieder, wenn vorhadnen, sonst rot.
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        public static System.Windows.Media.Brush DoesFtpDirectoryExist(string dirPath)
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(dirPath);
                request.Credentials = new NetworkCredential(Var.FtpUsername1, Var.FtpPassword1);
                request.Timeout     = 4000;
                request.Method      = WebRequestMethods.Ftp.ListDirectory;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                return(Brushes.Green);
            }
            catch (WebException ex)
            {
                IO.Log(2018061814, "DoesFtpDirectoryExist() | '" + dirPath + "' existiert nicht. | " + ex.Message);
                return(Brushes.Red);
            }
            catch (Exception ex)
            {
                IO.Log(2018062003, "DoesFtpDirectoryExist() | '" + dirPath + "' existiert nicht. | " + ex.GetType() + " | " + ex.Message);
                return(Brushes.Red);
            }
        }
示例#2
0
 /* Get the Date/Time a File was Created */
 public string GetFileCreatedDateTime(string fileName)
 {
     try
     {
         /* Create an FTP Request */
         ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
         /* Log in to the FTP Server with the User Name and Password Provided */
         ftpRequest.Credentials = new NetworkCredential(user, pass);
         /* When in doubt, use these options */
         ftpRequest.UseBinary  = true;
         ftpRequest.UsePassive = true;
         ftpRequest.KeepAlive  = true;
         /* Specify the Type of FTP Request */
         ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
         /* Establish Return Communication with the FTP Server */
         ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
         /* Establish Return Communication with the FTP Server */
         ftpStream = ftpResponse.GetResponseStream();
         /* Get the FTP Server's Response Stream */
         StreamReader ftpReader = new StreamReader(ftpStream);
         /* Store the Raw Response */
         string fileInfo = null;
         /* Read the Full Response Stream */
         try { fileInfo = ftpReader.ReadToEnd(); }
         catch (Exception ex) { IO.Log(2018061804, "ftp, getFileCreatedDateTime  | " + ex.ToString()); }
         /* Resource Cleanup */
         ftpReader.Close();
         ftpStream.Close();
         ftpResponse.Close();
         ftpRequest = null;
         /* Return File Created Date Time */
         return(fileInfo);
     }
     catch (Exception ex) { IO.Log(2018061805, "ftp, getFileCreatedDateTime  | " + ex.ToString()); }
     /* Return an Empty string Array if an Exception Occurs */
     return("");
 }
示例#3
0
        //Calling the method:
        //string ftpDirectory = "ftp://ftpserver.com/rootdir/test_if_exist_directory/"; //Note: backslash at the last position of the path.
        //bool dirExists = DoesFtpDirectoryExist(ftpDirectory);

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // von http://csharp-blog.de/2015/11/c-asyncawait-fortschritts-benachrichtigung-und-abbruch-moeglichkeit/

        public static async Task DownloadFileAsync(Uri ftpSite, string targetPath, IProgress <int> onProgressChanged, CancellationToken token)
        {
            try
            {
                //File.Create(Path.Combine(targetPath));

                int  totalBytes = 0;
                long fileSize   = 0;

                Thread.Sleep(Var.SleepingMilliSec);

                FtpWebRequest requestS = (FtpWebRequest)WebRequest.Create(ftpSite);
                requestS.Credentials = new NetworkCredential(Var.FtpUsername1, Var.FtpPassword1);
                requestS.Method      = WebRequestMethods.Ftp.GetFileSize;

                using (FtpWebResponse response = (FtpWebResponse)await requestS.GetResponseAsync())
                {
                    fileSize = response.ContentLength;
                    response.Close();

                    Thread.Sleep(Var.SleepingMilliSec);
                }

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpSite);
                request.Credentials = new NetworkCredential(Var.FtpUsername1, Var.FtpPassword1);
                request.Method      = WebRequestMethods.Ftp.DownloadFile;

                using (FtpWebResponse response = (FtpWebResponse)await request.GetResponseAsync())
                {
                    Stream data = response.GetResponseStream();

                    //Console.WriteLine($"Downloading {ftpSite.AbsoluteUri} to {targetPath}...");
                    IO.Log(2018061904, $"Starte Download von {ftpSite.AbsoluteUri} nach {targetPath}");

                    byte[] byteBuffer = new byte[4096];
                    using (FileStream output = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, useAsync: true))
                    {
                        int bytesRead = 1;
                        while (bytesRead > 0)
                        {
                            bytesRead = await data.ReadAsync(byteBuffer, 0, byteBuffer.Length);

                            if (bytesRead > 0)
                            {
                                totalBytes += bytesRead;

                                // Report progress
                                int locProgress = Convert.ToInt32(totalBytes * 100 / fileSize);
                                onProgressChanged?.Report(locProgress);
                                // Upload
                                await output.WriteAsync(byteBuffer, 0, bytesRead, token);
                            }
                        }
                    }

                    //Console.WriteLine($"Downloaded {ftpSite.AbsoluteUri} to {targetPath}");
                    IO.Log(2018061905, $"Download von {ftpSite.AbsoluteUri} nach {targetPath} war erfolgreich.");
                }
            }
            catch (OperationCanceledException)
            {
                //Console.Write("Download cancelled!");
                IO.Log(2018061906, "Download abgebrochen.");
            }
            catch (WebException e)
            {
                //Console.WriteLine($"Failed to download {ftpSite.AbsoluteUri} to {targetPath}");
                //Console.WriteLine(e);
                IO.Log(2018061907, $"Download fehlgeschlagen: {ftpSite.AbsoluteUri} nach {targetPath} : " + e.Message);
                throw;
            }
        }