public void IsHostAvailableTest()
        {
            var sw = PerformanceStopwatch.StartNew();

            var result = NetworkHelper.IsHostAvailable("www.google.com", timeout: 500);

            var pingTime = sw.StopReset();

            Debug.WriteLine($"Ping Time = {pingTime.TotalMilliseconds}");

            Assert.IsTrue(result);

            result = NetworkHelper.IsHostAvailable(RandomData.GenerateUrl(), 500);

            Assert.IsFalse(result);

            //Google
            result = NetworkHelper.IsHostAvailable("8.8.4.4", timeout: 500);

            Assert.IsTrue(result);

            result = NetworkHelper.IsHostAvailable("8.8.4.4");

            Assert.IsTrue(result);
        }
        /// <summary>
        /// Deletes file list.
        /// </summary>
        /// <param name="files">The file list to delete.</param>
        /// <returns>System.Int32 with the number of files that were successfully deleted.</returns>
        /// <remarks>Use the <seealso cref="Processed">Processed</seealso> event to find out if file deletion succeeded or failed.</remarks>
        public int DeleteFiles(IEnumerable <FileInfo> files)
        {
            Encapsulation.TryValidateParam(files, nameof(files));

            var successCount = 0;

            List <FileInfo> list = files.ToList();

            for (int i = 0; i < list.Count; i++)
            {
                FileInfo tempFile = list[i];
                if (tempFile.Exists)
                {
                    try
                    {
                        var psw = PerformanceStopwatch.StartNew();

                        // TODO: ADD ASYNC - await Task.Run(()=> method);
                        tempFile.Delete();

                        var perf = psw.StopReset();

                        successCount += 1;

                        this.OnProcessed(new FileProgressEventArgs
                        {
                            Name                = tempFile.FullName,
                            Message             = tempFile.Name,
                            ProgressState       = FileProgressState.Deleted,
                            Size                = tempFile.Length,
                            SpeedInMilliseconds = perf.TotalMilliseconds,
                        });
                    }
                    catch (Exception ex) when(ex is IOException || ex is SecurityException || ex is UnauthorizedAccessException)
                    {
                        this.OnProcessed(new FileProgressEventArgs
                        {
                            Name          = tempFile.FullName,
                            ProgressState = FileProgressState.Error,
                            Size          = tempFile.Length,
                            Message       = ex.Message,
                        });
                    }
                }
                else
                {
                    this.OnProcessed(new FileProgressEventArgs
                    {
                        Name          = tempFile.FullName,
                        ProgressState = FileProgressState.Error,
                        Message       = Resources.FileNotFound,
                    });
                }
            }

            return(successCount);
        }
        /// <summary>
        /// Deletes file list.
        /// </summary>
        /// <param name="files">The file list to delete.</param>
        /// <returns>System.Int32 with the number of files that were successfully deleted.</returns>
        /// <remarks>Use the <seealso cref="Processed">Processed</seealso> event to find out if file deletion succeeded or failed.</remarks>
        public int DeleteFiles(IEnumerable <FileInfo> files)
        {
            Encapsulation.TryValidateParam(files, nameof(files));

            var successCount = 0;

            foreach (var tempFile in files.AsParallel())
            {
                if (tempFile.Exists)
                {
                    try
                    {
                        var psw = PerformanceStopwatch.StartNew();

                        tempFile.Delete();

                        var perf = psw.StopReset();

                        successCount += 1;

                        OnProcessed(new FileProgressEventArgs
                        {
                            Name                = tempFile.FullName,
                            Message             = tempFile.Name,
                            ProgressState       = FileProgressState.Deleted,
                            Size                = tempFile.Length,
                            SpeedInMilliseconds = perf.TotalMilliseconds
                        });
                    }
                    catch (Exception ex) when(ex is IOException || ex is SecurityException ||
                                              ex is UnauthorizedAccessException)
                    {
                        OnProcessed(new FileProgressEventArgs
                        {
                            Name          = tempFile.FullName,
                            ProgressState = FileProgressState.Error,
                            Size          = tempFile.Length,
                            Message       = ex.Message
                        });
                    }
                }
                else
                {
                    OnProcessed(new FileProgressEventArgs
                    {
                        Name          = tempFile.FullName,
                        ProgressState = FileProgressState.Error,
                        Size          = tempFile.Length,
                        Message       = Resources.FileNotFound
                    });
                }
            }

            return(successCount);
        }
        /// <summary>
        /// Copies files to new location. Will not throw exceptions.
        /// </summary>
        /// <param name="files">The files.</param>
        /// <param name="destinationFolder">The destination folder.</param>
        /// <returns>System.Object.</returns>
        /// <remarks>Use the Processed event to find out if file copied succeeded or failed.</remarks>
        public int CopyFiles(IEnumerable <FileInfo> files, DirectoryInfo destinationFolder)
        {
            Encapsulation.TryValidateParam(files, nameof(files));
            Encapsulation.TryValidateParam <ArgumentNullException>(destinationFolder != null, nameof(destinationFolder));

            var successCount = 0;

            Thread.CurrentThread.Priority = ThreadPriority.Lowest;

            if (destinationFolder.Exists == false)
            {
                destinationFolder.Create();
            }

            foreach (var tempFile in files)
            {
                if (tempFile.Exists)
                {
                    try
                    {
                        var newFileName = new FileInfo(tempFile.FullName.Replace(tempFile.Directory.Root.FullName, destinationFolder.FullName));

                        if (newFileName.Directory.Exists == false)
                        {
                            newFileName.Directory.Create();
                        }

                        var psw = PerformanceStopwatch.StartNew();

                        tempFile.CopyTo(newFileName.FullName, true);

                        var perf = psw.StopReset();

                        successCount += 1;

                        this.OnProcessed(new FileProgressEventArgs
                        {
                            Name                = tempFile.FullName,
                            Message             = tempFile.Name,
                            ProgressState       = FileProgressState.Copied,
                            Size                = tempFile.Length,
                            SpeedInMilliseconds = perf.TotalMilliseconds
                        });
                    }
                    catch (Exception ex)
                    {
                        //Send error if an Exception happens.
                        this.OnProcessed(new FileProgressEventArgs
                        {
                            Name          = tempFile.FullName,
                            ProgressState = FileProgressState.Error,
                            Size          = tempFile.Length,
                            Message       = ex.Message
                        });
                    }
                }
                else
                {
                    this.OnProcessed(new FileProgressEventArgs
                    {
                        Name          = tempFile.FullName,
                        ProgressState = FileProgressState.Error,
                        Size          = tempFile.Length,
                        Message       = Resources.FileNotFound
                    });
                }
            }

            return(successCount);
        }
Beispiel #5
0
        public int CopyFiles([NotNull] IEnumerable <FileInfo> files, [NotNull] DirectoryInfo destination)
        {
            FileInfo[] list = files.ArgumentNotNull().ToArray();

            _ = destination.ArgumentNotNull().CheckExists();

            var destinationPath = destination.FullName;

            var successCount = 0;

            for (var fileCount = 0; fileCount < list.Length; fileCount++)
            {
                FileInfo tempFile = list[fileCount];

                if (tempFile.Exists)
                {
                    try
                    {
                        var newFileName = new FileInfo(fileName: tempFile.FullName.Replace(tempFile.Directory.Root.FullName, destinationPath, StringComparison.InvariantCulture));

                        if (newFileName.Directory.Exists is false)
                        {
                            newFileName.Directory.Create();
                        }

                        var psw = PerformanceStopwatch.StartNew();

                        _ = tempFile.CopyTo(newFileName.FullName, overwrite: true);

                        TimeSpan perf = psw.StopReset();

                        successCount += 1;

                        this.OnProcessed(new FileProgressEventArgs
                        {
                            Name                = tempFile.FullName,
                            Message             = tempFile.Name,
                            ProgressState       = FileProgressState.Copied,
                            Size                = tempFile.Length,
                            SpeedInMilliseconds = perf.TotalMilliseconds,
                        });
                    }
                    catch (Exception ex)
                    {
                        // Send error.
                        this.OnProcessed(new FileProgressEventArgs
                        {
                            Name          = tempFile.FullName,
                            ProgressState = FileProgressState.Error,
                            Size          = tempFile.Length,
                            Message       = ex.Message,
                        });
                    }
                }
                else
                {
                    this.OnProcessed(new FileProgressEventArgs
                    {
                        Name          = tempFile.FullName,
                        ProgressState = FileProgressState.Error,
                        Size          = tempFile.Length,
                        Message       = Resources.FileNotFound,
                    });
                }
            }

            return(successCount);
        }
Beispiel #6
0
        public int DeleteFiles([NotNull] IEnumerable <FileInfo> files)
        {
            if (files.Any() is false)
            {
                return(0);
            }

            var successCount = 0;

            FileInfo[] list = files.ToArray();

            for (var fileCount = 0; fileCount < list.Length; fileCount++)
            {
                FileInfo tempFile = list[fileCount];

                if (tempFile.Exists)
                {
                    try
                    {
                        var psw = PerformanceStopwatch.StartNew();

                        using (var task = new Task(() => tempFile.Delete()))
                        {
                            task.FireAndForget();                             //TODO: DOCUMENT THIS EXAMPLE
                        }

                        TimeSpan perf = psw.StopReset();

                        successCount += 1;

                        this.OnProcessed(new FileProgressEventArgs
                        {
                            Name                = tempFile.FullName,
                            Message             = tempFile.Name,
                            ProgressState       = FileProgressState.Deleted,
                            Size                = tempFile.Length,
                            SpeedInMilliseconds = perf.TotalMilliseconds,
                        });
                    }
                    catch (Exception ex) when(ex is IOException or SecurityException or UnauthorizedAccessException)
                    {
                        this.OnProcessed(new FileProgressEventArgs
                        {
                            Name          = tempFile.FullName,
                            ProgressState = FileProgressState.Error,
                            Size          = tempFile.Length,
                            Message       = ex.Message,
                        });
                    }
                }
                else
                {
                    this.OnProcessed(new FileProgressEventArgs
                    {
                        Name          = tempFile.FullName,
                        ProgressState = FileProgressState.Error,
                        Message       = Resources.FileNotFound,
                    });
                }
            }

            return(successCount);
        }
        /// <summary>
        /// Copies files to new location. Will not throw exceptions.
        /// </summary>
        /// <param name="files">The files.</param>
        /// <param name="destinationFolder">The destination folder.</param>
        /// <returns>System.Object.</returns>
        /// <remarks>Use the Processed event to find out if file copied succeeded or failed.</remarks>
        public int CopyFiles(IEnumerable <FileInfo> files, DirectoryInfo destinationFolder)
        {
            Encapsulation.TryValidateParam(files, nameof(files));
            Encapsulation.TryValidateParam <ArgumentNullException>(destinationFolder != null, nameof(destinationFolder));

            var successCount = 0;

            if (destinationFolder.Exists == false)
            {
                destinationFolder.Create();
            }

            List <FileInfo> list = files.ToList();

            for (int i = 0; i < list.Count; i++)
            {
                FileInfo tempFile = list[i];
                if (tempFile.Exists)
                {
                    try
                    {
                        var newFileName = new FileInfo(tempFile.FullName.Replace(tempFile.Directory.Root.FullName, destinationFolder.FullName));

                        if (newFileName.Directory.Exists == false)
                        {
                            newFileName.Directory.Create();
                        }

                        var psw = PerformanceStopwatch.StartNew();

                        tempFile.CopyTo(newFileName.FullName, overwrite: true);

                        var perf = psw.StopReset();

                        successCount += 1;

                        this.OnProcessed(new FileProgressEventArgs
                        {
                            Name                = tempFile.FullName,
                            Message             = tempFile.Name,
                            ProgressState       = FileProgressState.Copied,
                            Size                = tempFile.Length,
                            SpeedInMilliseconds = perf.TotalMilliseconds,
                        });
                    }
                    catch (Exception ex)
                    {
                        // Send error.
                        this.OnProcessed(new FileProgressEventArgs
                        {
                            Name          = tempFile.FullName,
                            ProgressState = FileProgressState.Error,
                            Size          = tempFile.Length,
                            Message       = ex.Message,
                        });
                    }
                }
                else
                {
                    this.OnProcessed(new FileProgressEventArgs
                    {
                        Name          = tempFile.FullName,
                        ProgressState = FileProgressState.Error,
                        Size          = tempFile.Length,
                        Message       = Resources.FileNotFound,
                    });
                }
            }

            return(successCount);
        }